I am having issues populating a List from deserilizatiopn from XML.
This is my class objects (on client):
[Serializable, XmlRoot("Groups"), XmlType("Groups")]
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlArray("Items")]
[XmlArrayItem("Group", typeof(Group))]
public List<Group> group { get; set; }
}
[XmlType("Group")]
public class Group
{
[XmlElement("GroupRef")]
public Guid GroupRef;
[XmlElement("Name")]
public string Name;
[XmlElement("Description")]
public string Description;
}
This is the code that Deserializethings:
XmlSerializer serializer = new XmlSerializer(typeof(Groups));
var groups = (Model.Groups)serializer.Deserialize(reader);
This is the XML:
<Groups>
<Group>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Todays Work</Name>
<Description>System</Description>
</Group>
<Group>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>All</Name>
<Description>System</Description>
</Group>
<Group>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Unassigned</Name>
<Description>System</Description>
</Group>
</Groups>
I get no error but the object count is zero?
Your input XML is in wrong format. If I construct the object and serialize it, I get
<Groups>
<Items>
<Group>
<GroupRef>bf2616d7-b98c-4743-8e25-2e0e101ab2da</GroupRef>
<Name>test</Name>
<Description>desc</Description>
</Group>
</Items>
</Groups>
So you should wrap each Group by as per your current design.
The problem is with the attributes used on the property group. Using the attributes you have defined, all the XML elements "Group" need to be contained within an element named "Items".
The solution is simple, just use the the XmlElement attribute as follows. Once you've changed the attribute to use XmlElement("Group") it should work.
public class Groups
{
public Groups()
{
group = new List<Group>();
}
[XmlElement("Group")]
public List<Group> group { get; set; }
}
Your XML
doesn't reflect the structure of class Groups
correctly. Change your XML
to this
<Groups>
<ArrayOfGroup>
<Group>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Todays Work</Name>
<Description>System</Description>
</Group>
<Group>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>All</Name>
<Description>System</Description>
</Group>
<Group>
<GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
<Name>Unassigned</Name>
<Description>System</Description>
</Group>
</ArrayOfGroup>
</Groups>
And the class definitions as
public class Groups
{
public Groups()
{
Group = new List<Group>();
}
[XmlArray("ArrayOfGroup")]
public List<Group> Group { get; set; }
}
public class Group
{
public Guid GroupRef;
public string Name;
public string Description;
}
This code will deserialize your XML properly
XmlSerializer serializer = new XmlSerializer(typeof(Groups));
StringReader reader = new StringReader(xmlString);
var groups = (Groups)serializer.Deserialize(reader);
OR, you can name the tag in XML as "Items" and rename the XmlArray
attribute to "Items" as you did originally.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.