在c#中通过反序列化将xml转换为列表

本文关键字:xml 转换 列表 反序列化 | 更新日期: 2023-09-27 17:54:24

我有一个XML文件,我正试图通过反序列化填充我的数据列表。

我的XML在这里

<?xml version="1.0" ?>
<CustomerQueryRs>
    <CustomerRet>
        <ListID>6BE0000-1159990808</ListID>
        <Name>+ Blaine Bailey</Name>
        <FullName>+ Blaine Bailey</FullName>
        <Phone>866-855-0800</Phone>
    </CustomerRet>
    <CustomerRet>
        <ListID>9BA0000-1165353294</ListID>
        <Name>+ Brian Boyd</Name>
        <FullName>+ Brian Boyd</FullName>
        <Phone>203-245-1877</Phone>
    </CustomerRet>
        <CustomerRet>
        <ListID>9280000-1164147562</ListID>
        <Name>+ Brian Leahy</Name>
        <FullName>+ Brian Leahy</FullName>
        <Phone>508-341-0955</Phone>
    </CustomerRet>
</CustomerQueryRs>

这里我给出了我的完整代码。我只是不明白为什么我的代码不工作…我的代码中缺少什么......它没有给出错误,但列表没有被填充。所以请告诉我代码中哪些地方需要修改

[XmlTypeAttribute(AnonymousType = true)]
public class CustomersData
{
    [XmlArray(ElementName = "CustomerQueryRs")]
    [XmlArrayItem(ElementName = "CustomerRet")]
    public List<Customer> Customers { get; set; }
    public CustomersData()
    {
        Customers = new List<Customer>();
    }
}
public class Customer
{
    [XmlElement(ElementName = "ListID")]
    public string ListID { get; set; }
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }
    [XmlElement(ElementName = "FullName")]
    public string FullName { get; set; }
    [XmlElement(ElementName = "Phone")]
    public string Phone { get; set; }
}

这是我的反序列化代码

private object DeserialzeXml(string xml)
    {
        var xmlSer = new XmlSerializer(typeof(CustomersData), new XmlRootAttribute("CustomerQueryRs"));
        var stringReader = new StringReader(xml);
        return xmlSer.Deserialize(stringReader);
    }

请帮…

在c#中通过反序列化将xml转换为列表

应该可以:

[XmlElement("CustomerRet")]
public List<Customer> Customers { get; set; }

和一个完整的例子:

[XmlTypeAttribute(AnonymousType = true)]
public class CustomersData
{
    [XmlElement("CustomerRet")]
    public List<Customer> Customers { get; set; }
    public CustomersData()
    {
        Customers = new List<Customer>();
    }
}
public class Customer
{
    [XmlElement(ElementName = "ListID")]
    public string ListID { get; set; }
    [XmlElement(ElementName = "Name")]
    public string Name { get; set; }
    [XmlElement(ElementName = "FullName")]
    public string FullName { get; set; }
    [XmlElement(ElementName = "Phone")]
    public string Phone { get; set; }
}
class Program
{
    static void Main()
    {
        var xml = 
@"<?xml version=""1.0"" ?>
<CustomerQueryRs>
  <CustomerRet>
    <ListID>6BE0000-1159990808</ListID>
    <Name>+ Blaine Bailey</Name>
    <FullName>+ Blaine Bailey</FullName>
    <Phone>866-855-0800</Phone>
  </CustomerRet>
  <CustomerRet>
    <ListID>9BA0000-1165353294</ListID>
    <Name>+ Brian Boyd</Name>
    <FullName>+ Brian Boyd</FullName>
    <Phone>203-245-1877</Phone>
  </CustomerRet>
  <CustomerRet>
    <ListID>9280000-1164147562</ListID>
    <Name>+ Brian Leahy</Name>
    <FullName>+ Brian Leahy</FullName>
    <Phone>508-341-0955</Phone>
  </CustomerRet>
</CustomerQueryRs>";
        var serializer = new XmlSerializer(typeof(CustomersData), new XmlRootAttribute("CustomerQueryRs"));
        using (var stringReader = new StringReader(xml))
        using (var reader = XmlReader.Create(stringReader))
        {
            var result = (CustomersData)serializer.Deserialize(reader);
            Console.WriteLine(result.Customers[1].FullName);
        }
    }
}

将xml转换为列表的可读代码

 string xmlString = System.IO.File.ReadAllText(@"C:'Users'user'Downloads'userDetail.xml");
 List<GridViewDetails> userDetail = (List<GridViewDetails>)ConvertXmlStringtoObject<List<GridViewDetails>>(xmlString);
  static T ConvertXmlStringtoObject<T>(string xmlString)
        {
            T classObject;
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            using (StringReader stringReader = new StringReader(xmlString))
            {
                classObject = (T)xmlSerializer.Deserialize(stringReader);
            }
            return classObject;
        }