UnityUtilities
UnityUtilities.XmlHelper Class Reference

The XmlHelper serializes and deserializes to/from XML and allows convenient access to optional element content and attributes when reading general XMLs. More...

Static Public Member Functions

static XmlSerializer GetSerializer< T > ()
 Gets a cached XmlSerializer for the specified type. More...
 
static string SerializeToXmlString< T > (this T data)
 Uses the XmlSerializer to serialize data into a string that can later be deserialized again via DeserializeFromXmlString<T>. More...
 
static T DeserializeFromXmlString< T > (this string str)
 Deserializes an object serialized with SerializeToXmlString<T>. More...
 
static string GetElementString (this XmlNode xmlNode, string name, string defaultValue="")
 Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned. More...
 
static int GetElementInt (this XmlNode xmlNode, string name, int defaultValue=0)
 Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned. More...
 
static float GetElementFloat (this XmlNode xmlNode, string name, float defaultValue=0)
 Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned. More...
 
static bool GetElementBool (this XmlNode xmlNode, string name, bool defaultValue=false)
 Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned. More...
 
static string GetAttributeString (this XmlNode xmlNode, string name, string defaultValue="")
 Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned. More...
 
static int GetAttributeInt (this XmlNode xmlNode, string name, int defaultValue=0)
 Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned. More...
 
static int GetAttributeIntNullable (this XmlNode xmlNode, string name, int? defaultValue=null)
 Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned. More...
 
static float GetAttributeFloat (this XmlNode xmlNode, string name, float defaultValue=0)
 Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned. More...
 
static float GetAttributeFloatNullable (this XmlNode xmlNode, string name, float? defaultValue=null)
 Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned. More...
 
static bool GetAttributeBool (this XmlNode xmlNode, string name, bool defaultValue=false)
 Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned. More...
 

Detailed Description

The XmlHelper serializes and deserializes to/from XML and allows convenient access to optional element content and attributes when reading general XMLs.

Member Function Documentation

§ DeserializeFromXmlString< T >()

static T UnityUtilities.XmlHelper.DeserializeFromXmlString< T > ( this string  str)
static

Deserializes an object serialized with SerializeToXmlString<T>.

Template Parameters
TThedata type that was serialized.
Parameters
strThe serialized data string.
Returns
The deserialized data.
Exceptions
InvalidOperationExceptionAn error occurred during deserialization. The original exception is available using the InnerException property.

§ GetAttributeBool()

static bool UnityUtilities.XmlHelper.GetAttributeBool ( this XmlNode  xmlNode,
string  name,
bool  defaultValue = false 
)
static

Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the attribute from.
nameThe name of the attribute.
defaultValueThe default value if no attribute with that name exists.
Returns
The content of the attribute if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.

§ GetAttributeFloat()

static float UnityUtilities.XmlHelper.GetAttributeFloat ( this XmlNode  xmlNode,
string  name,
float  defaultValue = 0 
)
static

Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the attribute from.
nameThe name of the attribute.
defaultValueThe default value if no attribute with that name exists.
Returns
The content of the attribute if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetAttributeFloatNullable()

static float UnityUtilities.XmlHelper.GetAttributeFloatNullable ( this XmlNode  xmlNode,
string  name,
float?  defaultValue = null 
)
static

Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the attribute from.
nameThe name of the attribute.
defaultValueThe default value if no attribute with that name exists.
Returns
The content of the attribute if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetAttributeInt()

static int UnityUtilities.XmlHelper.GetAttributeInt ( this XmlNode  xmlNode,
string  name,
int  defaultValue = 0 
)
static

Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the attribute from.
nameThe name of the attribute.
defaultValueThe default value if no attribute with that name exists.
Returns
The content of the attribute if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetAttributeIntNullable()

static int UnityUtilities.XmlHelper.GetAttributeIntNullable ( this XmlNode  xmlNode,
string  name,
int?  defaultValue = null 
)
static

Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the attribute from.
nameThe name of the attribute.
defaultValueThe default value if no attribute with that name exists.
Returns
The content of the attribute if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetAttributeString()

static string UnityUtilities.XmlHelper.GetAttributeString ( this XmlNode  xmlNode,
string  name,
string  defaultValue = "" 
)
static

Gets the content of first attribute with the specified name. If no attribute with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the attribute from.
nameThe name of the attribute.
defaultValueThe default value if no attribute with that name exists.
Returns
The content of the attribute if it exists; else the default value.

§ GetElementBool()

static bool UnityUtilities.XmlHelper.GetElementBool ( this XmlNode  xmlNode,
string  name,
bool  defaultValue = false 
)
static

Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the child from.
nameThe name of the child.
defaultValueThe default value if no child with that name exists.
Returns
The content of the child if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetElementFloat()

static float UnityUtilities.XmlHelper.GetElementFloat ( this XmlNode  xmlNode,
string  name,
float  defaultValue = 0 
)
static

Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the child from.
nameThe name of the child.
defaultValueThe default value if no child with that name exists.
Returns
The content of the child if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetElementInt()

static int UnityUtilities.XmlHelper.GetElementInt ( this XmlNode  xmlNode,
string  name,
int  defaultValue = 0 
)
static

Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the child from.
nameThe name of the child.
defaultValueThe default value if no child with that name exists.
Returns
The content of the child if it exists; else the default value.
Exceptions
FormatExceptionThe content is is not in the correct format.
OverflowExceptionThe content represents a number less than MinValue or greater than MaxValue.

§ GetElementString()

static string UnityUtilities.XmlHelper.GetElementString ( this XmlNode  xmlNode,
string  name,
string  defaultValue = "" 
)
static

Gets the content of first child element with the specified name. If no child with that name exists, the defaultValue is returned.

Parameters
xmlNodeThe XMlNode to get the child from.
nameThe name of the child.
defaultValueThe default value if no child with that name exists.
Returns
The content of the child if it exists; else the default value.

§ GetSerializer< T >()

static XmlSerializer UnityUtilities.XmlHelper.GetSerializer< T > ( )
static

Gets a cached XmlSerializer for the specified type.

Template Parameters
TThe type to get the XmlSerializer for.
Returns
An XmlSerializer for the specified type.

§ SerializeToXmlString< T >()

static string UnityUtilities.XmlHelper.SerializeToXmlString< T > ( this T  data)
static

Uses the XmlSerializer to serialize data into a string that can later be deserialized again via DeserializeFromXmlString<T>.

With a few exceptions (e.g. arrays of ArrayList and arrays of List<T>), all public attributes and fields of any public class should be serialized without any further need to tag the elements. The only thing needed is a public default constructor.

For finer control, see the MSDN document on XmlSerializer: https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx

Template Parameters
TThe data type to be serialized.
Parameters
dataThe data to be serialized.
Returns
The serialized data string.

The documentation for this class was generated from the following file: