- C# Serialize To Xml
- Jackson Xml Serialize List
- C# Serialize A List
- Xml Serialize List Without Root Element
Arrays and collections can be serialized to XML. The standard action when using the default serializer is for the name of the collection property to be added to the XML, with a contained element for each item named according to the items' data types. Serializing Arrays and Collections.
Definition
![Xml serialize list c# Xml serialize list c#](/uploads/1/2/4/9/124907745/206406068.png)
Serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.
XmlSerializerXmlSerializerXmlSerializerXmlSerializer
Examples
The following example contains two main classes:
PurchaseOrder
and Test
. The PurchaseOrder
class contains information about a single purchase. The Test
class contains the methods that create the purchase order, and that read the created purchase order.Remarks
XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization re-creates the object in its original state from the XML output. You can think of serialization as a way of saving the state of an object into a stream or buffer. For example, ASP.NET uses the XmlSerializer class to encode XML Web service messages.
Cod waw zombies ps3. The data in your objects is described using programming language constructs like classes, fields, properties, primitive types, arrays, and even embedded XML in the form of XmlElement or XmlAttribute objects. You have the option of creating your own classes, annotated with attributes, or using the XML Schema Definition Tool (Xsd.exe) to generate the classes based on an existing XML Schema definition (XSD) document. If you have an XML Schema, you can run the Xsd.exe to produce a set of classes that are strongly typed to the schema and annotated with attributes to adhere to the schema when serialized.
To transfer data between objects and XML requires a mapping from the programming language constructs to XML schema and from the XML schema to the programming language constructs. The XmlSerializer and related tools like Xsd.exe provide the bridge between these two technologies at both design time and runtime. At design time, use the Xsd.exe to produce an XML schema document (.xsd) from your custom classes or to produce classes from a given schema. In either case, the classes are annotated with custom attributes to instruct the XmlSerializer how to map between the XML schema system and the common language runtime. At runtime, instances of the classes can be serialized into XML documents that follow the given schema. Likewise, these XML documents can be deserialized into runtime objects. Note that the XML schema is optional, and not required at design time or runtime.
Controlling Generated XML
To control the generated XML, you can apply special attributes to classes and members. For example, to specify a different XML element name, apply an XmlElementAttribute to a public field or property, and set the ElementName property. For a complete list of similar attributes, see Attributes That Control XML Serialization. You can also implement the IXmlSerializable interface to control the XML output.
If the XML generated must conform to section 5 of the World Wide Consortium document, Simple Object Access Protocol (SOAP) 1.1, you must construct the XmlSerializer with an XmlTypeMapping. To further control the encoded SOAP XML, use the attributes listed in Attributes That Control Encoded SOAP Serialization.
With the XmlSerializer you can take advantage of working with strongly typed classes and still have the flexibility of XML. Using fields or properties of type XmlElement, XmlAttribute or XmlNode in your strongly typed classes, you can read parts of the XML document directly into XML objects.
If you work with extensible XML schemas, you can also use the XmlAnyElementAttribute and XmlAnyAttributeAttribute attributes to serialize and deserialize elements or attributes that are not found in the original schema. To use the objects, apply an XmlAnyElementAttribute to a field that returns an array of XmlElement objects, or apply an XmlAnyAttributeAttribute to a field that returns an array of XmlAttribute objects.
If a property or field returns a complex object (such as an array or a class instance), the XmlSerializer converts it to an element nested within the main XML document. For example, the first class in the following code returns an instance of the second class.
The serialized, XML output looks like this:
If a schema includes an element that is optional (minOccurs = '0'), or if the schema includes a default value, you have two options. One option is to use System.ComponentModel.DefaultValueAttribute to specify the default value, as shown in the following code.
Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of
propertyNameSpecified
. For example, if there is a field named 'MyFirstName' you would also create a field named 'MyFirstNameSpecified' that instructs the XmlSerializer whether to generate the XML element named 'MyFirstName'. This is shown in the following example.Overriding Default Serialization
You can also override the serialization of any set of objects and their fields and properties by creating one of the appropriate attributes, and adding it to an instance of the XmlAttributes class. Overriding serialization in this way has two uses: first, you can control and augment the serialization of objects found in a DLL, even if you do not have access to the source; second, you can create one set of serializable classes, but serialize the objects in multiple ways. For more details, see the XmlAttributeOverrides class and How to: Control Serialization of Derived Classes.
To serialize an object, call the Serialize method. To deserialize an object, call the Deserialize method.
To add XML namespaces to an XML document, see XmlSerializerNamespaces.
Note
The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection. A class that implements IEnumerable must implement a public
Add
method that takes a single parameter. The Add
method's parameter must be of the same type as is returned from the Current
property on the value returned from GetEnumerator
, or one of that type's bases. A class that implements ICollection (such as CollectionBase) in addition to IEnumerable must have a public Item
indexed property (indexer in C#) that takes an integer, and it must have a public Count
property of type integer. The parameter to the Add
method must be the same type as is returned from the Item
property, or one of that type's bases. For classes that implement ICollection, values to be serialized are retrieved from the indexed Item
property, not by calling GetEnumerator
.You must have permission to write to the temporary directory (as defined by the TEMP environment variable) to deserialize an object.
Dynamically Generated Assemblies
To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types. The infrastructure finds and reuses those assemblies. This behavior occurs only when using the following constructors:
If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance. The easiest solution is to use one of the previously mentioned two constructors. Otherwise, you must cache the assemblies in a Hashtable, as shown in the following example.
Serialization of ArrayList and Generic List
The XmlSerializer cannot serialize or deserialize the following:
- Arrays of ArrayList
- Arrays of List<T>
Serialization of Enumerations of Unsigned Long
The XmlSerializer cannot be instantiated to serialize an enumeration if the following conditions are true: The enumeration is of type unsigned long (
ulong
in C#) and the enumeration contains any member with a value larger than 9,223,372,036,854,775,807. For example, the following cannot be serialized.C# Serialize To Xml
Objects marked with the Obsolete Attribute no longer serialized
In the .NET Framework 3.5 the XmlSerializer class no longer serializes objects that are marked as
[Obsolete]
.Jackson Xml Serialize List
Constructors
XmlSerializer()XmlSerializer()XmlSerializer()XmlSerializer() | Initializes a new instance of the XmlSerializer class. |
XmlSerializer(Type)XmlSerializer(Type)XmlSerializer(Type)XmlSerializer(Type) | Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize XML documents into objects of the specified type. |
XmlSerializer(Type, String)XmlSerializer(Type, String)XmlSerializer(Type, String)XmlSerializer(Type, String) | Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize XML documents into objects of the specified type. Specifies the default namespace for all the XML elements. |
XmlSerializer(Type, Type[])XmlSerializer(Type, Type[])XmlSerializer(Type, Type[])XmlSerializer(Type, Type[]) | Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize XML documents into object of a specified type. If a property or field returns an array, the extraTypes parameter specifies objects that can be inserted into the array. |
XmlSerializer(Type, XmlAttributeOverrides)XmlSerializer(Type, XmlAttributeOverrides)XmlSerializer(Type, XmlAttributeOverrides)XmlSerializer(Type, XmlAttributeOverrides) | Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize XML documents into objects of the specified type. Each object to be serialized can itself contain instances of classes, which this overload can override with other classes. |
XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String) | Initializes a new instance of the XmlSerializer class that can serialize objects of type Object into XML document instances, and deserialize XML document instances into objects of type Object. Each object to be serialized can itself contain instances of classes, which this overload overrides with other classes. This overload also specifies the default namespace for all the XML elements and the class to use as the XML root element. |
XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String) | Initializes a new instance of the XmlSerializer class that can serialize objects of type Object into XML document instances, and deserialize XML document instances into objects of type Object. Each object to be serialized can itself contain instances of classes, which this overload overrides with other classes. This overload also specifies the default namespace for all the XML elements and the class to use as the XML root element. |
XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String, Evidence)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String, Evidence)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String, Evidence)XmlSerializer(Type, XmlAttributeOverrides, Type[], XmlRootAttribute, String, String, Evidence) | Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML document instances, and deserialize XML document instances into objects of the specified type. This overload allows you to supply other types that can be encountered during a serialization or deserialization operation, as well as a default namespace for all XML elements, the class to use as the XML root element, its location, and credentials required for access. |
XmlSerializer(Type, XmlRootAttribute)XmlSerializer(Type, XmlRootAttribute)XmlSerializer(Type, XmlRootAttribute)XmlSerializer(Type, XmlRootAttribute) | Initializes a new instance of the XmlSerializer class that can serialize objects of the specified type into XML documents, and deserialize an XML document into object of the specified type. It also specifies the class to use as the XML root element. |
XmlSerializer(XmlTypeMapping)XmlSerializer(XmlTypeMapping)XmlSerializer(XmlTypeMapping)XmlSerializer(XmlTypeMapping) | Initializes an instance of the XmlSerializer class using an object that maps one type to another. |
Methods
CanDeserialize(XmlReader)CanDeserialize(XmlReader)CanDeserialize(XmlReader)CanDeserialize(XmlReader) | Gets a value that indicates whether this XmlSerializer can deserialize a specified XML document. |
CreateReader()CreateReader()CreateReader()CreateReader() | Returns an object used to read the XML document to be serialized. |
CreateWriter()CreateWriter()CreateWriter()CreateWriter() | When overridden in a derived class, returns a writer used to serialize the object. |
Deserialize(Stream)Deserialize(Stream)Deserialize(Stream)Deserialize(Stream) | Deserializes the XML document contained by the specified Stream. |
Deserialize(TextReader)Deserialize(TextReader)Deserialize(TextReader)Deserialize(TextReader) | Deserializes the XML document contained by the specified TextReader. |
Deserialize(XmlReader)Deserialize(XmlReader)Deserialize(XmlReader)Deserialize(XmlReader) | Deserializes the XML document contained by the specified XmlReader. |
Deserialize(XmlReader, String)Deserialize(XmlReader, String)Deserialize(XmlReader, String)Deserialize(XmlReader, String) | Deserializes the XML document contained by the specified XmlReader and encoding style. |
Deserialize(XmlReader, String, XmlDeserializationEvents)Deserialize(XmlReader, String, XmlDeserializationEvents)Deserialize(XmlReader, String, XmlDeserializationEvents)Deserialize(XmlReader, String, XmlDeserializationEvents) | Deserializes the object using the data contained by the specified XmlReader. |
Deserialize(XmlReader, XmlDeserializationEvents)Deserialize(XmlReader, XmlDeserializationEvents)Deserialize(XmlReader, XmlDeserializationEvents)Deserialize(XmlReader, XmlDeserializationEvents) | Deserializes an XML document contained by the specified XmlReader and allows the overriding of events that occur during deserialization. |
Deserialize(XmlSerializationReader)Deserialize(XmlSerializationReader)Deserialize(XmlSerializationReader)Deserialize(XmlSerializationReader) | Deserializes the XML document contained by the specified XmlSerializationReader. |
Equals(Object)Equals(Object)Equals(Object)Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object) |
FromMappings(XmlMapping[])FromMappings(XmlMapping[])FromMappings(XmlMapping[])FromMappings(XmlMapping[]) | Returns an array of XmlSerializer objects created from an array of XmlTypeMapping objects. |
FromMappings(XmlMapping[], Evidence)FromMappings(XmlMapping[], Evidence)FromMappings(XmlMapping[], Evidence)FromMappings(XmlMapping[], Evidence) | Returns an instance of the XmlSerializer class created from mappings of one XML type to another. |
FromMappings(XmlMapping[], Type)FromMappings(XmlMapping[], Type)FromMappings(XmlMapping[], Type)FromMappings(XmlMapping[], Type) | Returns an instance of the XmlSerializer class from the specified mappings. |
FromTypes(Type[])FromTypes(Type[])FromTypes(Type[])FromTypes(Type[]) | Returns an array of XmlSerializer objects created from an array of types. |
GenerateSerializer(Type[], XmlMapping[])GenerateSerializer(Type[], XmlMapping[])GenerateSerializer(Type[], XmlMapping[])GenerateSerializer(Type[], XmlMapping[]) | Returns an assembly that contains custom-made serializers used to serialize or deserialize the specified type or types, using the specified mappings. |
GenerateSerializer(Type[], XmlMapping[], CompilerParameters)GenerateSerializer(Type[], XmlMapping[], CompilerParameters)GenerateSerializer(Type[], XmlMapping[], CompilerParameters)GenerateSerializer(Type[], XmlMapping[], CompilerParameters) | Returns an assembly that contains custom-made serializers used to serialize or deserialize the specified type or types, using the specified mappings and compiler settings and options. |
GetHashCode()GetHashCode()GetHashCode()GetHashCode() | Serves as the default hash function. (Inherited from Object) |
GetType()GetType()GetType()GetType() | Gets the Type of the current instance. (Inherited from Object) |
GetXmlSerializerAssemblyName(Type)GetXmlSerializerAssemblyName(Type)GetXmlSerializerAssemblyName(Type)GetXmlSerializerAssemblyName(Type) | Returns the name of the assembly that contains one or more versions of the XmlSerializer especially created to serialize or deserialize the specified type. |
GetXmlSerializerAssemblyName(Type, String)GetXmlSerializerAssemblyName(Type, String)GetXmlSerializerAssemblyName(Type, String)GetXmlSerializerAssemblyName(Type, String) | Returns the name of the assembly that contains the serializer for the specified type in the specified namespace. |
MemberwiseClone()MemberwiseClone()MemberwiseClone()MemberwiseClone() | Creates a shallow copy of the current Object. (Inherited from Object) |
Serialize(Object, XmlSerializationWriter)Serialize(Object, XmlSerializationWriter)Serialize(Object, XmlSerializationWriter)Serialize(Object, XmlSerializationWriter) | Serializes the specified Object and writes the XML document to a file using the specified XmlSerializationWriter. |
Serialize(Stream, Object)Serialize(Stream, Object)Serialize(Stream, Object)Serialize(Stream, Object) | Serializes the specified Object and writes the XML document to a file using the specified Stream. |
Serialize(Stream, Object, XmlSerializerNamespaces)Serialize(Stream, Object, XmlSerializerNamespaces)Serialize(Stream, Object, XmlSerializerNamespaces)Serialize(Stream, Object, XmlSerializerNamespaces) | Serializes the specified Object and writes the XML document to a file using the specified Stream that references the specified namespaces. |
Serialize(TextWriter, Object)Serialize(TextWriter, Object)Serialize(TextWriter, Object)Serialize(TextWriter, Object) | Serializes the specified Object and writes the XML document to a file using the specified TextWriter. |
Serialize(TextWriter, Object, XmlSerializerNamespaces)Serialize(TextWriter, Object, XmlSerializerNamespaces)Serialize(TextWriter, Object, XmlSerializerNamespaces)Serialize(TextWriter, Object, XmlSerializerNamespaces) | Serializes the specified Object and writes the XML document to a file using the specified TextWriter and references the specified namespaces. |
Serialize(XmlWriter, Object)Serialize(XmlWriter, Object)Serialize(XmlWriter, Object)Serialize(XmlWriter, Object) | Serializes the specified Object and writes the XML document to a file using the specified XmlWriter. |
Serialize(XmlWriter, Object, XmlSerializerNamespaces)Serialize(XmlWriter, Object, XmlSerializerNamespaces)Serialize(XmlWriter, Object, XmlSerializerNamespaces)Serialize(XmlWriter, Object, XmlSerializerNamespaces) | Serializes the specified Object and writes the XML document to a file using the specified XmlWriter and references the specified namespaces. |
Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)Serialize(XmlWriter, Object, XmlSerializerNamespaces, String)Serialize(XmlWriter, Object, XmlSerializerNamespaces, String) | Serializes the specified object and writes the XML document to a file using the specified XmlWriter and references the specified namespaces and encoding style. |
Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String)Serialize(XmlWriter, Object, XmlSerializerNamespaces, String, String) | Serializes the specified Object and writes the XML document to a file using the specified XmlWriter, XML namespaces, and encoding. |
ToString()ToString()ToString()ToString() | Returns a string that represents the current object. (Inherited from Object) |
Events
UnknownAttributeUnknownAttributeUnknownAttributeUnknownAttribute | Occurs when the XmlSerializer encounters an XML attribute of unknown type during deserialization. |
UnknownElementUnknownElementUnknownElementUnknownElement | Occurs when the XmlSerializer encounters an XML element of unknown type during deserialization. |
UnknownNodeUnknownNodeUnknownNodeUnknownNode | Occurs when the XmlSerializer encounters an XML node of unknown type during deserialization. |
UnreferencedObjectUnreferencedObjectUnreferencedObjectUnreferencedObject | Occurs during deserialization of a SOAP-encoded XML stream, when the XmlSerializer encounters a recognized type that is not used or is unreferenced. |
Applies to
Thread Safety
C# Serialize A List
This type is thread safe.
See also
Xml Serialize List Without Root Element
- XmlAttributeOverridesXmlAttributeOverridesXmlAttributeOverridesXmlAttributeOverrides
- XmlSerializerXmlSerializerXmlSerializerXmlSerializer