Class XmlSerializerUtil
Summary description for XmlSerializerUtil.
Inheritance
Namespace: ImageVault.Common.Lib.Xml.Serialization
Assembly: ImageVault.Common.dll
Syntax
public class XmlSerializerUtil : Object
Remarks
2010-09-28 dan: Created
Properties
ReferenceComparer
The reference comparer compares the two reference
Declaration
public static IReferenceObjectComparer ReferenceComparer { get; }
Property Value
Type | Description |
---|---|
IReferenceObjectComparer |
Methods
AddItemReference(Object, Object)
Adds the supplied object to the cache.
Declaration
public static void AddItemReference(object o, object cacheReference)
Parameters
Type | Name | Description |
---|---|---|
System.Object | o | The System.Object to add. |
System.Object | cacheReference | The reference object that is used to create the cache |
Clone<T>(T)
Clones the supplied item by serializing it to xml and back.
Declaration
public static T Clone<T>(T obj)
where T : class
Parameters
Type | Name | Description |
---|---|---|
T | obj |
Returns
Type | Description |
---|---|
T | A new object that is a copy of this instance. |
Type Parameters
Name | Description |
---|---|
T |
DeleteCache()
Deletes the current cache
Declaration
public static void DeleteCache()
DeserializeFromXml<T>(String)
Deserializes an xml text to an object.
Declaration
public static T DeserializeFromXml<T>(string xml)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.String | xml | The xml to deserialize |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
T | The type to serialize the xml to |
DeserializeFromXml<T>(String, XmlSerializer)
Deserializes an xml text to an object.
Declaration
public static T DeserializeFromXml<T>(string xml, XmlSerializer xser)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.String | xml | The xml to deserialize |
System.Xml.Serialization.XmlSerializer | xser | The System.Xml.Serialization.XmlSerializer to use or null if the default serializer for the type should be used |
Returns
Type | Description |
---|---|
T | The deserialized object. |
Type Parameters
Name | Description |
---|---|
T | The type to serialize the xml to |
DeserializeFromXmlFile<T>(String)
Deserializes a file (xml) to an object.
Declaration
public static T DeserializeFromXmlFile<T>(string path)
where T : class
Parameters
Type | Name | Description |
---|---|---|
System.String | path | The path to the file to deserialize. |
Returns
Type | Description |
---|---|
T | The deserialized |
Type Parameters
Name | Description |
---|---|
T |
DeserializeObject(Type, String)
Deserializes an object from a string to the object value
Declaration
public static object DeserializeObject(Type type, string data)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type to deserialize to |
System.String | data | The data used for deserialization |
Returns
Type | Description |
---|---|
System.Object | The deserialized object |
DeserializeObject(Type, XmlReader)
Deserializes an object of the specific type by reading data from the supplied reader.
Declaration
public static object DeserializeObject(Type type, XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The System.Type to deserialize to |
System.Xml.XmlReader | reader | The System.Xml.XmlReader to use as an input for the deserialization |
Returns
Type | Description |
---|---|
System.Object | The deserialized obejct |
Remarks
Uses the DeserializeObject(Type, XmlReader, Boolean) where expectWrapperElement is set to true.
DeserializeObject(Type, XmlReader, Boolean)
Deserializes an object of the specific type by reading data from the supplied reader.
Declaration
public static object DeserializeObject(Type type, XmlReader reader, bool expectWrapperElement)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The System.Type to deserialize to |
System.Xml.XmlReader | reader | The System.Xml.XmlReader to use as an input for the deserialization |
System.Boolean | expectWrapperElement | True if the object to be serialized is inside the current reader node, false if the reader node is positioined on the start element of the item |
Returns
Type | Description |
---|---|
System.Object | The deserialized obejct |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
System.Runtime.Serialization.SerializationException | If error in Deserialization data. Can't assign null to a ValueType.(. |
GetItemReference(XmlReader)
Gets the referred object of the current element.
Declaration
public static object GetItemReference(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlReader | reader |
Returns
Type | Description |
---|---|
System.Object | The referred object or null if there isn't any refered object. |
GetSerializer(Type)
Gets the serializer for the supplied type
Declaration
public static XmlSerializer GetSerializer(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The type to get the serializer for |
Returns
Type | Description |
---|---|
System.Xml.Serialization.XmlSerializer | The Types serializer |
GetSerializer(Type, Type)
Gets the XmlSerializer for the specified type
Declaration
public static XmlSerializer GetSerializer(Type type, Type extraType)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The System.Type to get the XmlSerializer for |
System.Type | extraType | An extra System.Type that the XmlSerializer can handle. |
Returns
Type | Description |
---|---|
System.Xml.Serialization.XmlSerializer | The System.Xml.Serialization.XmlSerializer |
InitCacheForReading(Object)
Initiates a XmlSerializerCache for a deserialize operation.
Declaration
public static void InitCacheForReading(object refObject)
Parameters
Type | Name | Description |
---|---|---|
System.Object | refObject |
Remarks
When the deserialize operation is completed, don't forget to do a DeleteCache() call to enable other serialize/deserialize methods to work.
Exceptions
Type | Condition |
---|---|
System.ApplicationException | If a XmlSerializerCache already exists in the thread. |
System.ArgumentNullException | If |
ItemReferenceDone(Object)
Removes the cache if the supplied object is the same as the one that initialized the cache.
Declaration
public static void ItemReferenceDone(object o)
Parameters
Type | Name | Description |
---|---|---|
System.Object | o | The object to check. |
ReadAttribute<T>(XmlReader, String)
Reads an attribute from the current XmlReader
Declaration
public static T ReadAttribute<T>(XmlReader reader, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlReader | reader | The System.Xml.XmlReader to read from |
System.String | name | The name of the attribute to read |
Returns
Type | Description |
---|---|
T | The read attribute value |
Type Parameters
Name | Description |
---|---|
T | The type of value to read |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
System.Xml.XmlException | If no attribute with the supplied name was found . |
ReadToNextElement(XmlReader)
Moves the reader to the next element.(or end element)
Declaration
public static bool ReadToNextElement(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlReader | reader | The System.Xml.XmlReader to read from. |
Returns
Type | Description |
---|---|
System.Boolean | True if we was able to reach the next element, false otherwize. |
Remarks
If the reader already is placed on an end element, no advancement is done and false is returned.
ReadTypeInfo(XmlReader)
Reads the type information from the reader.
Declaration
public static Type ReadTypeInfo(XmlReader reader)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlReader | reader | The System.Xml.XmlReader to read from |
Returns
Type | Description |
---|---|
System.Type | The found type |
Exceptions
Type | Condition |
---|---|
System.Runtime.Serialization.SerializationException | If the type attribute was not found. |
System.TypeLoadException | If the type was not found. |
RetrieveItem(XmlReader, IXmlSerializerMemberInfo)
Reads an item from the supplied xml reader of the supplied type and name
Declaration
public static object RetrieveItem(XmlReader reader, IXmlSerializerMemberInfo member)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlReader | reader | The System.Xml.XmlReader to read from |
IXmlSerializerMemberInfo | member | The IXmlSerializerMemberInfo defining the property that should recieve the item. |
Returns
Type | Description |
---|---|
System.Object |
RetrieveItem(XmlReader, Type, String)
Reads an item from the supplied xml reader of the supplied type and name
Declaration
public static object RetrieveItem(XmlReader reader, Type type, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlReader | reader | The System.Xml.XmlReader to read from |
System.Type | type | The System.Type of object to read. |
System.String | name | The name of the element to read, if null comparison will not occur (any name is ok) |
Returns
Type | Description |
---|---|
System.Object | The read object. |
SerializeObject(Object)
Serializes the object
Declaration
public static string SerializeObject(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
Returns
Type | Description |
---|---|
System.String | The Serialized object |
SerializeObject(XmlWriter, Object)
Serializes the supplied object to the writer.
Declaration
public static void SerializeObject(XmlWriter writer, object o)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Xml.XmlWriter to serialize the object to |
System.Object | o | The System.Object to serialize. |
SerializeObject(XmlWriter, Object, Type)
Serializes the supplied object to the writer.
Declaration
public static void SerializeObject(XmlWriter writer, object obj, Type baseType)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Xml.XmlWriter to serialize the object to |
System.Object | obj | The System.Object to serialize. |
System.Type | baseType | The base System.Type to serialize |
SerializeToXml(Object)
Serializes an object to Xml
Declaration
public static string SerializeToXml(object obj)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
Returns
Type | Description |
---|---|
System.String | The serialized xml |
SerializeToXml(Object, Encoding)
Serializes an object to Xml
Declaration
public static string SerializeToXml(object obj, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
System.Text.Encoding | encoding | The System.Text.Encoding to use for the xml serialized string |
Returns
Type | Description |
---|---|
System.String | The serialized xml |
SerializeToXml(Object, XmlSerializer)
Serializes an object to Xml
Declaration
public static string SerializeToXml(object obj, XmlSerializer xser)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
System.Xml.Serialization.XmlSerializer | xser | The System.Xml.Serialization.XmlSerializer to use or null if the default should be used |
Returns
Type | Description |
---|---|
System.String | The serialized xml |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
SerializeToXml(Object, XmlSerializer, Encoding)
Serializes an object to Xml
Declaration
public static string SerializeToXml(object obj, XmlSerializer xser, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
System.Xml.Serialization.XmlSerializer | xser | The System.Xml.Serialization.XmlSerializer to use or null if the default should be used |
System.Text.Encoding | encoding | The System.Text.Encoding to use for the xml serialized string |
Returns
Type | Description |
---|---|
System.String | The serialized xml |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
SerializeToXmlFile(Object, String)
Serializes an object to Xml and stores it in a file
Declaration
public static void SerializeToXmlFile(object obj, string path)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
System.String | path | The path to the file to write to. If it exists, it will be overwritten. |
SerializeToXmlFile(Object, String, Encoding)
Serializes an object to Xml and stores it in a file
Declaration
public static void SerializeToXmlFile(object obj, string path, Encoding encoding)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to serialize |
System.String | path | The path to the file to write to. If it exists, it will be overwritten. |
System.Text.Encoding | encoding | The System.Text.Encoding to use |
StoreItemReference(Object, XmlWriter)
Writes the reference id to the current node and ensures that the supplied object is contained in the cache.
Declaration
public static bool StoreItemReference(object o, XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
System.Object | o | The System.Object to check/add. |
System.Xml.XmlWriter | writer | The System.Xml.XmlWriter used for serialization |
Returns
Type | Description |
---|---|
System.Boolean | True if the object has already been added to the cache, false otherwize. |
WriteAttribute(XmlWriter, Object, String)
Writes the supplied value as an attribute
Declaration
public static void WriteAttribute(XmlWriter writer, object value, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Xml.XmlWriter to write to |
System.Object | value | The value to write |
System.String | name | The name of the attribute to write |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If |
WriteItem(XmlWriter, Object, IXmlSerializerMemberInfo)
Writes an item to the supplied writer
Declaration
public static void WriteItem(XmlWriter writer, object o, IXmlSerializerMemberInfo member)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Object to write to |
System.Object | o | The System.Xml.XmlWriter to write |
IXmlSerializerMemberInfo | member | The IXmlSerializerMemberInfo containing information on how to write the data. |
WriteItem(XmlWriter, Object, String)
Writes an item to the supplied writer
Declaration
public static void WriteItem(XmlWriter writer, object o, string name)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Object to write to |
System.Object | o | The System.Xml.XmlWriter to write |
System.String | name | The name of the element |
WriteItem(XmlWriter, Object, String, Dictionary<String, String>)
Writes an item to the supplied writer
Declaration
public static void WriteItem(XmlWriter writer, object o, string name, Dictionary<string, string> attributes)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Xml.XmlWriter to write to |
System.Object | o | The System.Object to write |
System.String | name | The name of the element |
System.Collections.Generic.Dictionary<System.String, System.String> | attributes | The attributes to write to the element. |
WriteItem(XmlWriter, Object, String, Type)
Writes an item to the supplied writer
Declaration
public static void WriteItem(XmlWriter writer, object o, string name, Type baseType)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Object to write to |
System.Object | o | The System.Xml.XmlWriter to write |
System.String | name | The name of the element |
System.Type | baseType | The type of object that is the base type |
WriteItem(XmlWriter, Object, String, Type, Dictionary<String, String>)
Writes an item to the supplied writer
Declaration
public static void WriteItem(XmlWriter writer, object o, string name, Type baseType, Dictionary<string, string> attributes)
Parameters
Type | Name | Description |
---|---|---|
System.Xml.XmlWriter | writer | The System.Object to write to |
System.Object | o | The System.Xml.XmlWriter to write |
System.String | name | The name of the element |
System.Type | baseType | The type of object that is the base type |
System.Collections.Generic.Dictionary<System.String, System.String> | attributes | The attributes to write to the element. |
WriteTypeInfo(Object, XmlWriter)
Writes the type info on the current node (as an argument
Declaration
public static void WriteTypeInfo(object obj, XmlWriter writer)
Parameters
Type | Name | Description |
---|---|---|
System.Object | obj | The object to write the type for |
System.Xml.XmlWriter | writer | The System.Xml.XmlWriter to write to. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | If obj is null |
System.ArgumentNullException | If writer is null |