views:

18

answers:

2

Hi guys,

I have an xml file, something like this:

<root>
   <groups>
      <group />
      <group />
   </groups>
</root>

Now, I want to make a class something like this:

[XmlRoot]
public class Root
{
   [XmlElement("groups")]
   public Groups Groups { get; set; }
}

The problem is that the XmlElement attribute will make all the collection items named "groups" and I end up with something like this instead:

<root>
   <groups />
   <groups />
</root>

And if I leave the XmlElement attribute off, it will actually print the file with the correcy nodes, but the name of the node has captialized:

<root>
   <Groups>
      <group />
      <group />
   </Groups>
</root>

This is very annoying. How can I tell it to make the name in lowercase (or whatever name I want) for the xml file but to have it as Pascal case in my source code whilst at the same time specifying what the names of the sub elements should be?

Thanks

A: 

Can you show the Groups class code?

Serghei
A: 

If I understood you correctly, to read this XML file you need to use XmlArrayAttribute and XmlArrayItemAttribute.

[XmlRoot]
public class Root
{
    [XmlArray("groups")]
    [XmlArrayItem("group")]
    public List<Group> Groups { get; set; }
}

[XmlType]
public class Group
{
}
Athari
Lovely job! Thanks a lot!
Matt
@Matt If you accept the answer, please click on the ckeck mark near it to mark it as accepted.
Athari