C# 我需要知道如何将特定的XML反序列化为C中自定义类中定义的对象#

C# 我需要知道如何将特定的XML反序列化为C中自定义类中定义的对象#,c#,.net,xml,serialization,xml-serialization,C#,.net,Xml,Serialization,Xml Serialization,给定以下XML: <?xml version="1.0" encoding="UTF-8"?> <userAttributeList> <attribute> <userId>12345678</userId> <attId>1234</attId> <attName>Group</attName>

给定以下XML:

   <?xml version="1.0" encoding="UTF-8"?>
   <userAttributeList>
       <attribute>
          <userId>12345678</userId>
          <attId>1234</attId>
          <attName>Group</attName>
          <attTypeId>8</attTypeId>
          <attTypeName>User Group</attTypeName>
          <attData>Member</attData>
       </attribute>
       <attribute>
          <userId>12345678</userId>
          <attId>1235</attId>
          <attName>Contact Name</attName>
          <attTypeId>16</attTypeId>
          <attTypeName>Contact Center Greeting</attTypeName>
          <attData>John Smith</attData>
      </attribute>
      ...
    </userAttributeList>

我的代码编译时没有错误,但是返回的UserAttributeList没有显示子“属性”项。不会抛出任何错误

我宁愿这样做:

public class userAttributeList
{
    [XmlElement]
    public List<UserAttribute> attribute { get; set; }

    public UserAttributeList()
    {
        attribute = new List<UserAttribute>();
    }
}

public class UserAttribute
{
    public int userId { get; set; }
    public int attId { get; set; }
    public string attName { get; set; }
    public int attTypeId { get; set; }
    public string attTypeName { get; set; }
    public string attData { get; set; }
}
公共类userAttributeList
{
[XmlElement]
公共列表属性{get;set;}
公共用户属性列表()
{
属性=新列表();
}
}
公共类用户属性
{
public int userId{get;set;}
公共int attId{get;set;}
公共字符串attName{get;set;}
公共int attTypeId{get;set;}
公共字符串attTypeName{get;set;}
公共字符串attData{get;set;}
}

我宁愿做以下事情:

public class userAttributeList
{
    [XmlElement]
    public List<UserAttribute> attribute { get; set; }

    public UserAttributeList()
    {
        attribute = new List<UserAttribute>();
    }
}

public class UserAttribute
{
    public int userId { get; set; }
    public int attId { get; set; }
    public string attName { get; set; }
    public int attTypeId { get; set; }
    public string attTypeName { get; set; }
    public string attData { get; set; }
}
公共类userAttributeList
{
[XmlElement]
公共列表属性{get;set;}
公共用户属性列表()
{
属性=新列表();
}
}
公共类用户属性
{
public int userId{get;set;}
公共int attId{get;set;}
公共字符串attName{get;set;}
公共int attTypeId{get;set;}
公共字符串attTypeName{get;set;}
公共字符串attData{get;set;}
}
我个人会使用。获取现有xml,从中生成xsd,然后使用LinqToXsd将该xml加载到LinqToXsd对象中。然后你可以做如下事情:

xml.company.com.reports.report.Load(xmlFileContents); 
建立一个POCO

我个人会用。获取现有xml,从中生成xsd,然后使用LinqToXsd将该xml加载到LinqToXsd对象中。然后你可以做如下事情:

xml.company.com.reports.report.Load(xmlFileContents); 

建立一个POCO

我知道事情会很简单。我想我让自己太难了。谢谢我知道事情会很简单。我想我让自己太难了。谢谢