Hello there,
I need to design classes, that can be serialized to XML like this:
<?xml version="1.0"?>
<Groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Group>
<Item name="Group 1 item 1" />
<Item name="Group 1 item 2" />
</Group>
<Group>
<Item name="Group 2 item 1" />
<Item name="Group 2 item 2" />
<Item name="Group 2 item 3" />
</Group>
<Group>
<Item name="Group 3 item 1" />
</Group>
</Groups>
Each node should be represented by C# class, and I have a problem
because:
[Serializable]
public class Groups : List<Group>
{
}
[Serializable]
public class Group : List<Item>
{
}
[Serializable]
public class Item
{
private string name;
[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
gives:
<?xml version="1.0"?>
<ArrayOfArrayOfItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfItem>
<Item name="Group 1 item 1" />
<Item name="Group 1 item 2" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 2 item 1" />
<Item name="Group 2 item 2" />
<Item name="Group 2 item 3" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 3 item 1" />
</ArrayOfItem>
</ArrayOfArrayOfItem>
I was able to get rid of ArrayOfArrayOfItem with this:
[Serializable]
[XmlRoot (ElementName = "Groups")]
public class Groups : List<Group>
{
}
[Serializable]
public class Group : List<Item>
{
}
[Serializable]
public class Item
{
private string name;
[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
which gives:
<?xml version="1.0"?>
<Groups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ArrayOfItem>
<Item name="Group 1 item 1" />
<Item name="Group 1 item 2" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 2 item 1" />
<Item name="Group 2 item 2" />
<Item name="Group 2 item 3" />
</ArrayOfItem>
<ArrayOfItem>
<Item name="Group 3 item 1" />
</ArrayOfItem>
</Groups>
but I cannot change then name of inner "ArrayOfItem". The problem is
mainly, that I don't want to introduce to Groups class any property
member for Group collection - the name for such property will be
strange (either the main class is named Groups and the property
somehow else or the main class name is Foo and the property is
Groups). I know that the goal (from XML point of view) can be achieved
with something like below, but I don't like this 'Elements':
[Serializable]
public class Groups
{
private List<Groupelements = new List<Group();
[XmlElement (ElementName = "Group")]
public List<GroupElements
{
get
{
return this.elements;
}
}
}
[Serializable]
public class Group
{
private List<Itemelements = new List<Item();
[XmlElement (ElementName = "Item")]
public List<ItemElements
{
get
{
return this.elements;
}
}
}
[Serializable]
public class Item
{
private string name;
[XmlAttribute (AttributeName="name")]
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
}
Thank you & best regards