all groups > dotnet xml > april 2006 >
You're in the

dotnet xml

group:

XML Serialization of ArrayList causes extra nodes


XML Serialization of ArrayList causes extra nodes dfontanesi NO[at]SPAM gmail.com
4/28/2006 12:35:07 PM
dotnet xml:
I'm trying to serialize a class that contains an ArrayList...something
like this:

[Serializable]
public class Units
{
private ArrayList _units;
private string _description;

[XmlArray(ElementName="units")]
[XmlArrayItem("unit", typeof(unit))]
public ArrayList units
{
get{ return _units; }
set{ _units = value; }
}
public string description
{
get{ return _description;}
set{ _description = value; }
}
}

What I get when I serialize this is:

<units>
<units>
<description>test</description>
<unit>
// unit elements go here
</unit>
<unit>
// unit elements go here
</unit>
</units>
</units>

(too many units nodes...one for the class and one for the ArrayList)

What I want is:

<units>
<description>test</description>
<unit>
// unit elements go here
</unit>
<unit>
// unit elements go here
</unit>
</units>
Re: XML Serialization of ArrayList causes extra nodes dfontanesi NO[at]SPAM gmail.com
4/28/2006 12:44:29 PM
I guess the simple question is:

How do I prevent the ArrayList from spitting out the containing node?
Re: XML Serialization of ArrayList causes extra nodes Ion Vasilian
5/1/2006 12:54:24 PM
Take a look at XmlElementAttribute
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconattributesthatcontrolserialization.asp

Your code should be changed to something similar to:
[XmlElement("unit")]
public ArrayList units {
get { return _units;}
set { _unit = value; }
}

Ion

[quoted text, click to view]

AddThis Social Bookmark Button