Wimdows.NET

Wim's .NET blog

Xml Serialization and the XmlArray and XmlArrayItem control attributes

We all know -or should know- that when using XML Serialization either over SOAP or manually, we can control the serialized output with so called control attributes.

Well known examples are the XmlElementAttribute and the XmlAttributeAttribute. I think it's worth mentioning the usefulness of the XmlArrayAttribute and the XmlArrayItemAttribute as well. XmlArrayAttribute to be used on the usual fixed arrays, since dynamic arrays can't be serialized and the XmlArrayItemAttribute for the element name of a single item in the array.

Here's a sample class:

using System.Xml.Serialization;
[Serializable]
public class Customer
{ // we need default ctor for Serialization!
public Customer()
{}
private string __fname;
private string __lname;
private Invoice[] __invoices;




[XmlElement("FirstName")]
public string FName
{
get { return this.__fname; }
set { this.__fname=value; }
}

[XmlElement("LastName")]
public string LName
{
get { return this.__lname; }
set { this.__lname=value; }
}

[XmlArray("Orders")]
[XmlArrayItem("Order")]
public Invoice[] Invoices
{
get { return this.__invoices; }
set { this.__invoices=value; }
}
}

Without taking into account that an Invoice is actually not the same as an Order :-), when we look at the serialized XML for a Customer object, it looks roughly like this:

<Customer>
 <FirstName>Wim</FirstName>
 <LastName>Hollebrandse</LastName>
 <Orders>
  <Order>
   <ID>12312</ID>
  </Order>
  <Order>
   <ID>12313</ID>
  </Order>
  <Order>
   <ID>12314</ID>
  </Order>
 </Orders>
</Customer>

Oops - just noticed I typed 'ID' with two capitals. Check out the discussion on Andrew Stevenson's blog.

Posted: Aug 12 2004, 09:10 PM by Wim | with 8 comment(s)
Filed under: ,

Comments

kavitha said:

Good and helped me a lot

# September 19, 2007 4:40 AM

Kevin said:

Great, now how do you unwrap the array?  So instead of:

<Orders>

 <Order>

  <ID>12312</ID>

 </Order>

 <Order>

  <ID>12313</ID>

 </Order>

 <Order>

  <ID>12314</ID>

 </Order>

</Orders>

you have:

 <Order>

  <ID>12312</ID>

 </Order>

 <Order>

  <ID>12313</ID>

 </Order>

 <Order>

  <ID>12314</ID>

 </Order>

# February 3, 2009 5:07 PM

espresso said:

Good stuff.  Would be good if you showed a parent node and how to deal with that as well.

# February 7, 2009 1:08 AM

Enrique said:

If you want to unwrap the array you just use XmlElemnt. like this:

[XmlElement("Order")]

public Invoice[] Invoices

{

get { return this.__invoices; }

set { this.__invoices=value; }

}

XmlElemnt doesn't need to be just one.

# March 26, 2009 5:07 PM

amrish said:

i want like this format for serialize class,,,

<?xml version="1.0"?>

<Mathslook>

 <Textbox>

   <Box>

     <Name>ty</Name>

     <x_axis>90</x_axis>

     <Font>

       <Y_axis>0</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

   <Font color="red">

     <Y_axis>67.8</Y_axis>

     <z_axis>0</z_axis>

   </Font>

 </Textbox>

 <Plot>

   <Box>

     <Name>\P</Name>

     <x_axis>457</x_axis>

     <Font>

       <Y_axis>10</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

 </Plot>

 <Link>

   <Box>

     <Name>\L</Name>

     <x_axis>103</x_axis>

     <Font>

       <Y_axis>14</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

   <Box>

     <Name>\L</Name>

     <x_axis>255</x_axis>

     <Font>

       <Y_axis>10</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

   <Box>

     <Name>\L</Name>

     <x_axis>253</x_axis>

     <Font>

       <Y_axis>10</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

   <Box>

     <Name>\L</Name>

     <x_axis>313</x_axis>

     <Font>

       <Y_axis>10</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

   <Box>

     <Name>\L</Name>

     <x_axis>160</x_axis>

     <Font>

       <Y_axis>10</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

   <Box>

     <Name>\L</Name>

     <x_axis>147</x_axis>

     <Font>

       <Y_axis>10</Y_axis>

       <z_axis>0</z_axis>

     </Font>

   </Box>

     </Link>

</Mathslook>

# February 9, 2011 4:24 AM

Mescalito said:

Thanks a lot ! I can create and parse a better XML file !

# February 23, 2012 3:47 AM