XML serialization code for an array of ASP.net objects

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.