Xml Serialize List

  1. C# Xml Serialize List Keyvaluepair
  2. Xml Serialize List Of Interface
-->

When you serialize an object that contains arrays or collections to XML, an element is created based upon the name of the property or field. This contains one child element for each item in the array. The names of the child elements match the data type names for the items. This blog is in continuation of my other blog.In the first blog, I explained how to use XML attribute overrides to reduce the number of attributes required in source code, and how to centralize some common functionality in a single location (the blog focused on how to serialize to XML using camel casing as default for tag names). 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: XmlSerializer.XmlSerializer(Type) XmlSerializer.XmlSerializer(Type, String). Serializing and DeSerializing Objects as XML in VB.NET It was unbelievably difficult to find a working example where the XML could come from a web server on a different domain. So, I guess I will share with the world how I managed to make it work. Hello everybody, I need to serialize a list of string text to xml and then desearialze it. List looks like 10, 6, 7, 11 and etc. How should I do this?

Attributes can be used to control the XML serialization of an object or to create an alternate XML stream from the same set of classes. For more details about creating an alternate XML stream, see How to: Specify an Alternate Element Name for an XML Stream.

Note

If the XML generated must conform to section 5 of the World Wide Web Consortium (W3C) document titled Simple Object Access Protocol (SOAP) 1.1, use the attributes listed in Attributes That Control Encoded SOAP Serialization.

By default, an XML element name is determined by the class or member name. In a simple class named Book, a field named ISBN will produce an XML element tag <ISBN>, as shown in the following example.

This default behavior can be changed if you want to give the element a new name. The following code shows how an attribute enables this by setting the ElementName property of a XmlElementAttribute.

For more information about attributes, see Attributes. For a list of attributes that control XML serialization, see Attributes That Control XML Serialization.

Controlling Array Serialization

The XmlArrayAttribute and the XmlArrayItemAttribute attributes are designed to control the serialization of arrays. Using these attributes, you can control the element name, namespace, and XML Schema (XSD) data type (as defined in the World Wide Web Consortium [www.w3.org] document titled 'XML Schema Part 2: Datatypes'). You can also specify the types that can be included in an array.

Serialize

The XmlArrayAttribute will determine the properties of the enclosing XML element that results when an array is serialized. For example, by default, serializing the array below will result in an XML element named Employees. The Employees element will contain a series of elements named after the array type Employee.

A serialized instance might resemble the following.

By applying a XmlArrayAttribute, you can change the name of the XML element, as follows.

The resulting XML might resemble the following.

The XmlArrayItemAttribute, on the other hand, controls how the items contained in the array are serialized. Note that the attribute is applied to the field returning the array.

The resulting XML might resemble the following.

Serializing Derived Classes

Root

Another use of the XmlArrayItemAttribute is to allow the serialization of derived classes. For example, another class named Manager that derives from Employee can be added to the previous example. If you do not apply the XmlArrayItemAttribute, the code will fail at run time because the derived class type will not be recognized. To remedy this, apply the attribute twice, each time setting the Type property for each acceptable type (base and derived).

A serialized instance might resemble the following.

Serializing an Array as a Sequence of Elements

You can also serialize an array as a flat sequence of XML elements by applying a XmlElementAttribute to the field returning the array as follows.

A serialized instance might resemble the following.

Another way to differentiate the two XML streams is to use the XML Schema Definition tool to generate the XML Schema (XSD) document files from the compiled code. (For more details on using the tool, see The XML Schema Definition Tool and XML Serialization.) When no attribute is applied to the field, the schema describes the element in the following manner.

When the XmlElementAttribute is applied to the field, the resulting schema describes the element as follows.

Xml Serialize List

Serializing an ArrayList

The ArrayList class can contain a collection of diverse objects. You can therefore use a ArrayList much as you use an array. Instead of creating a field that returns an array of typed objects, however, you can create a field that returns a single ArrayList. However, as with arrays, you must inform the XmlSerializer of the types of objects the ArrayList contains. To accomplish this, assign multiple instances of the XmlElementAttribute to the field, as shown in the following example.

Controlling Serialization of Classes Using XmlRootAttribute and XmlTypeAttribute

There are two attributes that can be applied to a class (and only a class): XmlRootAttribute and XmlTypeAttribute. These attributes are very similar. The XmlRootAttribute can be applied to only one class: the class that, when serialized, represents the XML document's opening and closing element—in other words, the root element. The XmlTypeAttribute, on the other hand, can be applied to any class, including the root class.

For example, in the previous examples, the Group class is the root class, and all its public fields and properties become the XML elements found in the XML document. Therefore, there can be only one root class. By applying the XmlRootAttribute, you can control the XML stream generated by the XmlSerializer. For example, you can change the element name and namespace.

The XmlTypeAttribute allows you to control the schema of the generated XML. This capability is useful when you need to publish the schema through an XML Web service. The following example applies both the XmlTypeAttribute and the XmlRootAttribute to the same class.

If this class is compiled, and the XML Schema Definition tool is used to generate its schema, you would find the following XML describing Group.

In contrast, if you were to serialize an instance of the class, only NewGroupName would be found in the XML document.

Preventing Serialization with the XmlIgnoreAttribute

C# Xml Serialize List Keyvaluepair

There might be situations when a public property or field does not need to be serialized. For example, a field or property could be used to contain metadata. In such cases, apply the XmlIgnoreAttribute to the field or property and the XmlSerializer will skip over it.

Xml Serialize List Of Interface

See also