Class XmlSerializer
- Namespace
- NMF.Serialization
- Assembly
- NMF.Serialization.dll
Class to serialize objects in a Xml-format.
public class XmlSerializer : Serializer
- Inheritance
-
XmlSerializer
- Derived
- Inherited Members
Constructors
XmlSerializer()
Creates a new XmlSerializer with default settings and no preloaded types
public XmlSerializer()
XmlSerializer(XmlSerializationSettings)
Creates a new XmlSerializer with the specified settings
public XmlSerializer(XmlSerializationSettings settings)
Parameters
settings
XmlSerializationSettingsSerializer-settings for the serializer. Can be null or Nothing in Visual Basic. In this case, the default settings will be used.
XmlSerializer(XmlSerializationSettings, IEnumerable<Type>)
Creates a new XmlSerializer with the specified settings and the given preloaded types
public XmlSerializer(XmlSerializationSettings settings, IEnumerable<Type> additionalTypes)
Parameters
settings
XmlSerializationSettingsThe settings to use for the serializer
additionalTypes
IEnumerable<Type>Set of types to load into the serializer
Remarks
The types will be loaded with the specified settings
XmlSerializer(XmlSerializer)
Creates a new XmlSerializer and copies settings and known types from the given serializer
public XmlSerializer(XmlSerializer parent)
Parameters
parent
XmlSerializerAn XML serializer to copy settings and known type information from
XmlSerializer(XmlSerializer, XmlSerializationSettings)
Creates a new XmlSerializer and copies settings and known types from the given serializer
public XmlSerializer(XmlSerializer parent, XmlSerializationSettings settings)
Parameters
parent
XmlSerializerAn XML serializer to copy settings and known type information from
settings
XmlSerializationSettingsNew settings
XmlSerializer(XmlSerializer, XmlSerializationSettings, IEnumerable<Type>)
Creates a new XmlSerializer and copies settings and known types from the given serializer
public XmlSerializer(XmlSerializer parent, XmlSerializationSettings settings, IEnumerable<Type> additionalTypes)
Parameters
parent
XmlSerializerAn XML serializer to copy settings and known type information from
settings
XmlSerializationSettingsNew settings
additionalTypes
IEnumerable<Type>Set of types to load into the serializer
XmlSerializer(IEnumerable<Type>)
Creates a new XmlSerializer with default settings
public XmlSerializer(IEnumerable<Type> additionalTypes)
Parameters
additionalTypes
IEnumerable<Type>Set of types to preload into the serializer
Remarks
Types will be loaded with default settings
Methods
CreateObject(XmlReader, ITypeSerializationInfo, XmlSerializationContext)
Creates the object for the current position
protected virtual object CreateObject(XmlReader reader, ITypeSerializationInfo tsi, XmlSerializationContext context)
Parameters
reader
XmlReaderthe xml reader
tsi
ITypeSerializationInfothe type serialization information
context
XmlSerializationContextthe serialization context
Returns
- object
the deserialized object
CreateRoot(XmlReader)
Creates the root element
protected object CreateRoot(XmlReader reader)
Parameters
reader
XmlReaderThe xml reader
Returns
- object
The root element
Deserialize(Stream)
Deserializes an Xml-representation of an object back to the corresponding object
public object Deserialize(Stream stream)
Parameters
stream
StreamThe stream containg the Xml code
Returns
- object
The corresponding object
Deserialize(TextReader)
Deserializes an Xml-representation of an object back to the corresponding object
public object Deserialize(TextReader reader)
Parameters
reader
TextReaderA TextReader containg the Xml code
Returns
- object
The corresponding object
Deserialize(string)
Deserializes an Xml-representation of an object back to the corresponding object
public object Deserialize(string path)
Parameters
path
stringThe path to the Xml file containg the Xml code
Returns
- object
The corresponding object
Deserialize(XmlReader)
Deserializes an Xml-representation of an object back to the corresponding object
public object Deserialize(XmlReader reader)
Parameters
reader
XmlReaderA XmlReader containing the Xml code
Returns
- object
The corresponding object
Remarks
The function will deserialize the object at the XmlReaders current position
GetElementTypeInfo(XmlReader, IPropertySerializationInfo)
Gets the type information for the current property
protected virtual ITypeSerializationInfo GetElementTypeInfo(XmlReader reader, IPropertySerializationInfo property)
Parameters
reader
XmlReaderThe xml reader
property
IPropertySerializationInfoThe current property
Returns
- ITypeSerializationInfo
The type serialization info that should be used in the remainder
GetRootElementTypeInfo(XmlReader)
Gets the type information for the root element
protected virtual ITypeSerializationInfo GetRootElementTypeInfo(XmlReader reader)
Parameters
reader
XmlReaderThe xml reader
Returns
- ITypeSerializationInfo
The type serialization info for the root element
GoToPropertyContent(XmlReader)
Moves the reader to the content of the property
protected virtual bool GoToPropertyContent(XmlReader reader)
Parameters
reader
XmlReaderthe Xml reader
Returns
- bool
true, if the reader could be moved sucessfully, otherwise false
HandleUnknownAttribute(XmlReader, object, ITypeSerializationInfo, XmlSerializationContext)
Handles an attribute that was not known to the serializer
protected virtual void HandleUnknownAttribute(XmlReader reader, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
reader
XmlReaderThe current reader position
obj
objectThe object that is currently deserialized
info
ITypeSerializationInfoThe type serialization information of the object
context
XmlSerializationContextThe serialization context
HandleUnknownElement(XmlReader, object, ITypeSerializationInfo, XmlSerializationContext)
Handles an element that was not known to the serializer
protected virtual void HandleUnknownElement(XmlReader reader, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
reader
XmlReaderThe current reader position
obj
objectThe object that is currently deserialized
info
ITypeSerializationInfoThe type serialization information of the object
context
XmlSerializationContextThe serialization context
Initialize(XmlReader, object, XmlSerializationContext)
Initializes the given object with the xml code at the current position of the XmlReader
public void Initialize(XmlReader reader, object obj, XmlSerializationContext context)
Parameters
reader
XmlReaderThe XmlReader with the Xml code
obj
objectThe object to initialize
context
XmlSerializationContextThe serialization context
InitializeAttributeProperties(XmlReader, object, ITypeSerializationInfo, XmlSerializationContext)
Initializes the attribute properties from the current reader position
protected virtual void InitializeAttributeProperties(XmlReader reader, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
reader
XmlReaderthe xml reader
obj
objectthe object
info
ITypeSerializationInfothe type serialization information
context
XmlSerializationContextthe serialization context
InitializeElementProperties(XmlReader, ref object, ITypeSerializationInfo, XmlSerializationContext)
Initializes the element properties from the xml reader position
protected virtual void InitializeElementProperties(XmlReader reader, ref object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
reader
XmlReaderthe xml reader
obj
objectthe element
info
ITypeSerializationInfothe type serialization information
context
XmlSerializationContextthe serialization context
InitializeProperty(XmlReader, IPropertySerializationInfo, object, XmlSerializationContext)
Initialized the property from the reader
protected virtual bool InitializeProperty(XmlReader reader, IPropertySerializationInfo property, object obj, XmlSerializationContext context)
Parameters
reader
XmlReaderthe xml reader
property
IPropertySerializationInfothe property
obj
objectthe object
context
XmlSerializationContextthe serialization context
Returns
- bool
true, if the initialization was successful, otherwise false
InitializePropertyFromText(IPropertySerializationInfo, object, string, XmlSerializationContext)
Initializes the given property from the provided text
protected virtual void InitializePropertyFromText(IPropertySerializationInfo property, object obj, string text, XmlSerializationContext context)
Parameters
property
IPropertySerializationInfoThe property
obj
objectThe object
text
stringThe input text
context
XmlSerializationContextThe serialization context
IsPropertyElement(XmlReader, IPropertySerializationInfo)
Determines whether the element at the current reader position refers to the given property
protected virtual bool IsPropertyElement(XmlReader reader, IPropertySerializationInfo property)
Parameters
reader
XmlReaderThe reader position
property
IPropertySerializationInfoThe property that should be tested
Returns
- bool
true, if the element is about the property, otherwise false
OverrideIdentifiedObject(object, XmlReader, XmlSerializationContext)
Determines whether the already identified element should be overridden
protected virtual bool OverrideIdentifiedObject(object obj, XmlReader reader, XmlSerializationContext context)
Parameters
obj
objectThe object that would be overridden
reader
XmlReaderThe current reader position
context
XmlSerializationContextThe serialization context
Returns
- bool
true, if the element shall be overridden, otherwise false
Serialize(object, Stream, bool)
Serializes the given object
public void Serialize(object source, Stream stream, bool fragment = false)
Parameters
source
objectThe object to be serialized
stream
StreamThe stream for the resulting Xml-code
fragment
boolA value that indicates whether the serializer should write a document definition
Serialize(object, TextWriter)
Serializes the given object
public void Serialize(object source, TextWriter writer)
Parameters
source
objectThe object to be serialized
writer
TextWriterThe TextWriter to write the Xml-code on
Serialize(object, TextWriter, bool)
Serializes the given object
public void Serialize(object source, TextWriter target, bool fragment)
Parameters
source
objectThe object to be serialized
target
TextWriterThe TextWriter to write the Xml-code on
fragment
boolA value that indicates whether the serializer should write a document definition
Serialize(object, string, bool)
Serializes the given object
public void Serialize(object obj, string path, bool fragment = false)
Parameters
obj
objectThe object to be serialized
path
stringThe path for the resulting Xml-file
fragment
boolA value that indicates whether the serializer should write a document definition
Serialize(object, XmlWriter)
Serializes the given object
public void Serialize(object source, XmlWriter writer)
Parameters
Serialize(object, XmlWriter, IPropertySerializationInfo, bool, XmlIdentificationMode, XmlSerializationContext)
Serializes the given object
public virtual void Serialize(object obj, XmlWriter writer, IPropertySerializationInfo property, bool writeInstance, XmlIdentificationMode identificationMode, XmlSerializationContext context)
Parameters
obj
objectThe object to be serialized
writer
XmlWriterThe XmlWriter to write the Xml-code on
property
IPropertySerializationInfoThe property for which the object is serialized
writeInstance
boolA value that indicates whether the serializer should write the element definition
identificationMode
XmlIdentificationModeA value indicating whether it is allowed to the serializer to use identifier
context
XmlSerializationContextThe serialization context
Remarks
If a converter is provided that is able to convert the object to string and convert the string back to this object, just the string-conversion is printed out
Serialize(object, XmlWriter, bool)
Serializes the given object
public void Serialize(object source, XmlWriter target, bool fragment)
Parameters
source
objectThe object to be serialized
target
XmlWriterThe XmlWriter to write the Xml-code on
fragment
boolA value that indicates whether the serializer should write a document definition
WriteAttributeProperties(XmlWriter, object, ITypeSerializationInfo, XmlSerializationContext)
Writes the attribute properties of the given object
protected virtual void WriteAttributeProperties(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
context
XmlSerializationContextThe serialization context
WriteAttributeValue(XmlWriter, object, object, IPropertySerializationInfo, XmlSerializationContext)
Writes the attribute value to the given writer
protected virtual void WriteAttributeValue(XmlWriter writer, object obj, object value, IPropertySerializationInfo property, XmlSerializationContext context)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
value
objectThe value of the attribute
property
IPropertySerializationInfoThe property serialization information
context
XmlSerializationContextThe serialization context
WriteBeginElement(XmlWriter, object, ITypeSerializationInfo)
Writes the beginning of an element
protected virtual void WriteBeginElement(XmlWriter writer, object obj, ITypeSerializationInfo info)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
WriteBeginRootElement(XmlWriter, object, ITypeSerializationInfo)
Writes the root element to the given writer
protected virtual void WriteBeginRootElement(XmlWriter writer, object root, ITypeSerializationInfo info)
Parameters
writer
XmlWriterThe xml writer to write to
root
objectThe root element
info
ITypeSerializationInfoThe serialization information of the objects type
WriteCollectionMembers(XmlWriter, object, ITypeSerializationInfo, XmlSerializationContext)
Writes the elements of the given collection to the provided writer
protected virtual void WriteCollectionMembers(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
context
XmlSerializationContextThe serialization context
WriteConstructorProperties(XmlWriter, object, ITypeSerializationInfo, XmlSerializationContext)
Writes the properties necessary for the constrctor call of this element
protected virtual void WriteConstructorProperties(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
context
XmlSerializationContextThe serialization context
WriteElementProperties(XmlWriter, object, ITypeSerializationInfo, XmlSerializationContext)
Writes the element properties of the given object to the provided writer
protected virtual void WriteElementProperties(XmlWriter writer, object obj, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
context
XmlSerializationContextThe serialization context
WriteEndElement(XmlWriter, object, ITypeSerializationInfo)
Completes the current element for the provided object
protected virtual void WriteEndElement(XmlWriter writer, object obj, ITypeSerializationInfo info)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
WriteEndRootElement(XmlWriter, object, ITypeSerializationInfo)
Completes the root element
protected virtual void WriteEndRootElement(XmlWriter writer, object root, ITypeSerializationInfo info)
Parameters
writer
XmlWriterThe xml writer to write to
root
objectThe element
info
ITypeSerializationInfoThe serialization information of the objects type
WriteIdentifiedObject(XmlWriter, object, XmlIdentificationMode, ITypeSerializationInfo, XmlSerializationContext)
Writes the provided identified object
protected virtual bool WriteIdentifiedObject(XmlWriter writer, object obj, XmlIdentificationMode identificationMode, ITypeSerializationInfo info, XmlSerializationContext context)
Parameters
writer
XmlWriterThe xml writer to write to
obj
objectThe element
identificationMode
XmlIdentificationModeThe identification mode for the current object
info
ITypeSerializationInfoThe serialization information of the objects type
context
XmlSerializationContextThe serialization context
Returns
- bool
true, if the object could be written as identified object, otherwise false