KittyHawk wrote:
How would I set up a class or classes to serialize to XML in the format
below? I cannot figure out how to serialize the group of links as "id"
attributes of the link element.
<data>
<links>
<link id="1" />
<link id="2" />
</links>
<time>12:00</time>
</data?
Ignoring the time element (which should be clear) you can use e.g.
[XmlRoot(ElementName="data")]
public class Data
{
public Data() { }
[XmlArray(ElementName="links")]
[XmlArrayItem(ElementName="link")]
public Link[] Links;
}
public class Link
{
public Link() { }
[XmlAttribute(AttributeName = "id")]
public string Id;
}
then you get e.g.
Data data = new Data();
Link link1 = new Link();
link1.Id = "1";
Link link2 = new Link();
link2.Id = "2";
data.Links = new Link[] { link1, link2 };
XmlSerializer serializer = new XmlSerializer(typeof(Data));
serializer.Serialize(Console.Out, data);
output as
<data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<links>
<link id="1" />
<link id="2" />
</links>
</data>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/