Serializing an XmlArray with attributes

G

Guest

Hi

I'm currently having trouble serializing an object to Xml, in the format I
need.
Below is the object definition.

-------------------------------
[Serializable]
public class Folders
{
protected List<PathString> _include;
protected List<PathString> _exclude;

public Folders()
{
_include = new List<PathString>();
_exclude = new List<PathString>();
}

[XmlArrayItem("Include")]
public List<PathString> Include
{
get { return _include; }
set { _include = value; }
}

[XmlArrayItem(ElementName="Exclude",Type=typeof(PathString))]
public List<PathString> Excludes
{
get { return _exclude; }
set { _exclude = value; }
}
}

[Serializable]
public class PathString
{
protected string _path;

[XmlAttribute]
public string Path
{
get { return _path; }
set { _path = value; }
}

public PathString()
{
}

public PathString(string path)
{
_path = path;
}
}
--------------------------

From these objects, I want my Xml output to look like :

<Folders>
<Include Path="Path1"/>
<Include Path="Path2"/>
<Exclude Path="LessTrodden"/>
</Folders>

However, I am having trouble getting the correct output.
The code above produces the following :

<Folders>
<Include>
<Include Path="Path1"/>
<Include Path="Path2"/>
</Include>
<Excludes>
<Exclude Path="LessTrodden"/>
</Excludes>
</Folders>

I want to use List (or IList) as I want to bind my object to a DevExpress
TreeList
Does anyone have an idea of how I can get my required output ?

Cheers

Ged
 
A

Alberto Poblacion

[...] I want my Xml output to look like :

<Folders>
<Include Path="Path1"/>
<Include Path="Path2"/>
<Exclude Path="LessTrodden"/>
</Folders>


I have not tried this out, but it is my understanding that you can
achieve the result that you desire by applying the attribute [XmlElement] to
the Include and Exclude lists. At least, this is documented to work with
arrays:

"If you apply the XmlElementAttribute to a field or property that returns an
array, the items in the array are encoded as a sequence of XML elements."
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.aspx
 
G

Guest

Alberto

Thanks for answering so quickly.
That's exactly what I needed.

I was so busy looking for an XmlArray attribute to fix it, I didn't even
look at the XmlElement attribute

Cheers

Ged


Alberto Poblacion said:
[...] I want my Xml output to look like :

<Folders>
<Include Path="Path1"/>
<Include Path="Path2"/>
<Exclude Path="LessTrodden"/>
</Folders>


I have not tried this out, but it is my understanding that you can
achieve the result that you desire by applying the attribute [XmlElement]
to the Include and Exclude lists. At least, this is documented to work
with arrays:

"If you apply the XmlElementAttribute to a field or property that returns
an array, the items in the array are encoded as a sequence of XML
elements."
http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlelementattribute.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top