简体   繁体   中英

C# - How to deserialize xml response

I am retrieving xml data from an web api and deserializing the data into objects.:

<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>

My current code is this:

[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; }
}

But, This code does not working correctly. Only first 2 Vendor elements are deserialized, Product element is not deserialized. What am i doing wrong?

best regards

You have few things to be taken care.

Make only one element XmlRoot . Refer below link and search for "Controlling Serialization of Classes Using XmlRootAttribute and XmlTypeAttribute".

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

Here in your case, the "Result" class is root.

Make rest all XmlType .

In the vendor class instead of "XmlArrayItem", make it just "XmlElement".

Here is the working code.

    [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; }
    }

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.

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