简体   繁体   English

我的反序列化XML至List <>为空

[英]My Deserialize XML to List<> is empty

I am having issues populating a List from deserilizatiopn from XML. 我在从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: 这是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. 您输入的XML格式错误。 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". 使用定义的属性,所有XML元素“ Group”都必须包含在名为“ Items”的元素中。

The solution is simple, just use the the XmlElement attribute as follows. 解决方案很简单,只需使用XmlElement属性,如下所示。 Once you've changed the attribute to use XmlElement("Group") it should work. 更改属性以使用XmlElement(“ Group”)后,它应该起作用。

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. 您的XML不能正确反映类Groups的结构。 Change your XML to this 将您的XML更改为此

<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 此代码将正确反序列化您的XML

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. 或者,您可以将XML中的标记命名为“ Items”,然后像最初一样将XmlArray属性重命名为“ Items”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM