【问题标题】:C# - How to deserialize xml responseC# - 如何反序列化 xml 响应
【发布时间】:2018-05-14 17:01:38
【问题描述】:

我正在从 Web api 检索 xml 数据并将数据反序列化为对象。:

<Result>
  <VendorInfo xml:lang="xx">
    <Vendor vname="A" cpe="B">
      <Product pname="C" cpe="D"/>
    </Vendor>
    <Vendor vname="E" cpe="F">
      <Product pname="G" cpe="H"/>
    </Vendor>
    <Vendor vname="I" cpe="J">
      <Product pname="K" cpe="L"/>
      <Product pname="M" cpe="N"/>
    </Vendor>
  </VendorInfo>
  <Status keyword="hoge" feed="bar"/>
</Result>

我当前的代码是这样的:

[XmlRoot(ElementName = "Product")]
public class Product
{
    [XmlAttribute(AttributeName = "pname")]
    public string Pname { get; set; }
    [XmlAttribute(AttributeName = "cpe")]
    public string Cpe { get; set; }
}

[XmlRoot(ElementName = "Vendor")]
public class Vendor
{
    [XmlArrayItem(ElementName = "Product")]
    public List<Product> Product { get; set; }
    [XmlAttribute(AttributeName = "vname")]
    public string Vname { get; set; }
    [XmlAttribute(AttributeName = "cpe")]
    public string Cpe { get; set; }
}

[XmlRoot(ElementName = "VendorInfo")]
public class VendorInfo
{
    [XmlElement(ElementName = "Vendor")]
    public List<Vendor> Vendor { get; set; }
    [XmlAttribute(AttributeName = "lang")]
    public string Lang { get; set; }
}

[XmlRoot(ElementName = "Status")]
public class Status
{
    [XmlAttribute(AttributeName = "feed")]
    public string Feed { get; set; }
    [XmlAttribute(AttributeName = "keyword")]
    public string Keyword { get; set; }
}

[XmlRoot(ElementName = "Result")]
public class Result
{
    [XmlElement(ElementName = "VendorInfo")]
    public VendorInfo VendorInfo { get; set; }
    [XmlElement(ElementName = "Status")]
    public Status Status { get; set; }
}

但是,此代码无法正常工作。 只有前 2 个供应商元素被反序列化,产品元素未被反序列化。 我做错了什么?

最好的问候

【问题讨论】:

标签: c# .net xml serialization xml-serialization


【解决方案1】:

你几乎没有什么需要注意的。

只制作一个元素XmlRoot。请参考以下链接并搜索“使用 XmlRootAttribute 和 XmlTypeAttribute 控制类的序列化”。

https://docs.microsoft.com/en-us/dotnet/standard/serialization/controlling-xml-serialization-using-attributes

在您的情况下,“结果”类是根。

大家休息XmlType

在供应商类而不是“XmlArrayItem”中,将其设为“XmlElement”。

这是工作代码。

    [XmlType("Product")]
    public class Product
    {
        [XmlAttribute(AttributeName = "pname")]
        public string Pname { get; set; }
        [XmlAttribute(AttributeName = "cpe")]
        public string Cpe { get; set; }
    }

    [XmlType("Vendor")]
    public class Vendor
    {
        [XmlElement(ElementName = "Product")]
        public List<Product> Product { get; set; }
        [XmlAttribute(AttributeName = "vname")]
        public string Vname { get; set; }
        [XmlAttribute(AttributeName = "cpe")]
        public string Cpe { get; set; }
    }

    [XmlType("VendorInfo")]
    public class VendorInfo
    {
        [XmlElement(ElementName = "Vendor")]
        public List<Vendor> Vendor { get; set; }
        [XmlAttribute(AttributeName = "xml:lang")]
        public string Lang { get; set; }
    }

    [XmlType("Status")]
    public class Status
    {
        [XmlAttribute(AttributeName = "feed")]
        public string Feed { get; set; }
        [XmlAttribute(AttributeName = "keyword")]
        public string Keyword { get; set; }
    }

    [XmlRoot(ElementName = "Result")]
    public class Result
    {
        [XmlElement(ElementName = "VendorInfo")]
        public VendorInfo VendorInfo { get; set; }
        [XmlElement(ElementName = "Status")]
        public Status Status { get; set; }
    }

【讨论】:

  • XmlRoot 是正确的。唯一需要改变的是 XmlArrayItem。
  • 是的....但是我提供的文档链接明确表示不要使用.... XmlRoot 用于多个类....
  • 首先文档说 XmlRootAtrribute 而不是 XmlRoot。其次,我多次使用 XmlRoot 不止一个类并且它有效。 Microsoft 网页和已知有很多错误。微软可能聘请了技术作家来编写那些从未编程过的网页。不要相信微软所说的一切。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多