views:

69

answers:

2

I've got a class like this:

[XmlRoot("channel")]
public class Channel
{
   [XmlElement("title")]
   public String Title { get; set; }

   [WhatElseGoesHere]
   [XmlArrayItem("item")]
   public List<Item> Items { get; set; }
}

My desired output is this:

<channel>
   <title>The title</title>
   <item>{item content}</item>
   <item>{item content}</item>
</channel>
+1  A: 

You can try using XmlElementAttribute instead of XmlArrayItemElementAttribute:

[XmlRoot("channel")]
public class Channel
{
    [XmlElement("title")]
    public String Title { get; set; }

    [XmlElement("item")]
    public List<Item> Items { get; set; }
}
Darin Dimitrov
Wow, that was really stupidly simple... I never would've thought to try using XmlElementAttribute. Thanks!
Daniel Schaffer
For the record, I actually tried Marc's answer first because I was "positive" this wouldn't work... and of course, it more or less generated your answer.
Daniel Schaffer
+2  A: 

You could start with xml and work back? Put the desired xml in a file (foo.xml), then:

xsd foo.xml
xsd foo.xsd /classes
notepad foo.cs

This usually provides an answer - although you can usually do the same thing different ways.

Marc Gravell
Nice trick! It ended up generating darin's answer (more or less)
Daniel Schaffer