XML serialization of an array requires the XML node name of the array element to be specified using XmlArrayAttribute and Xmlarrayitemattribute;xmlarrayattribute. XmlArrayItemAttribute the XML node name of the specified array element.
The following code example:
/
* Jade Open Technology Blog http://www.111cn.net * *
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Xml.Serialization;
Namespace Usexmlserialization
{
Class Program
{
static void Main (string[] args)
{
Declaring a Cat object
var cwhite = new Cat {Color = "white", Speed = ten, saying = "White or black, so long as the cat can catch mice, it is a Good cat "};
var cblack = new Cat {Color = "Black", Speed = ten, saying = "White or black, so long as the cat can catch mice, it is a Good cat "};
catcollection cc = new Catcollection {Cats = new cat[] {cwhite,cblack}};
Serialization of this object
XmlSerializer serializer = new XmlSerializer (typeof (Catcollection));
Serializing an object to the console
Serializer. Serialize (Console.Out, CC);
Console.read ();
}
}
[XmlRoot ("Cats")]
public class Catcollection
{
[XmlArray ("Items"), XmlArrayItem ("item")]
Public cat[] Cats {get; set;}
}
[XmlRoot ("Cat")]
public class Cat
{
Defines a property that is serialized as a cat node for a color property
[XmlAttribute ("Color")]
public string Color {get; set;}
Requires no serialization of speed properties
[XmlIgnore]
public int Speed {get; set;}
To set the saying property to be serialized as an XML child element
[XmlElement ("saying")]
public string saying {get; set;}
}
}<span style= "Font-family:verdana, Arial, Helvetica, Sans-serif" face= "Verdana, Arial, Helvetica, Sans-serif" > <span class=apple-style-span style= "FONT-SIZE:14PX; line-height:21px; White-space:normal ">
</SPAN></SPAN>
The above code will output:
<?xml version= "1.0" encoding= "gb2312"
<cats xmlns:xsi= "http://www.w3.org/2001/" Xmlschema-instance "xmlns:xsd=" HTTP://WW
W.w3.org/2001/xmlschema
<items>
<item color= "white"
<saying>white or black, so long as the "C" At can catch mice, it is a good
Cat</saying>
</item>
& Lt;item color= "Black"
<saying>white or black, so long as the cat can CA TCH mice, It is a good
Cat</saying>
</item>
</items>
&L T;/cats>