C#XML用于嵌入对象的对象

时间:2016-03-24 12:31:50

标签: c# xml serialization

鉴于下面的C#类和XML,如何使用嵌入的Comp对象列表将XML反序列化为RentRangeAPI类?

public class Comp
{
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string Rent { get; set; }
    public string Dist { get; set; }
}

public class RentRangeAPI
{
    public string ErrorCode { get; set; }
    public string ErrorInfo { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string Sqft { get; set; }
    public List<Comp> Comps { get; set; }
}

<rentrangeAPI>
   <ErrorCode>0</ErrorCode>
   <ErrorInfo>Success</ErrorInfo>
   <Address>123 Elm St.</Address>
   <City>Boulder</City>
   <State>CO</State>
   <Zipcode>80305</Zipcode>
   <Sqft>1000</Sqft>
   <Comps>
      <Comp>
         <Address>234 Elm St</Address>
         <City>Memphis</City>
         <State>TN</State>
         <Zipcode>38115</Zipcode>
         <Rent>925</Rent>
         <Dist>.17</Dist>
      </Comp>
      <Comp>
         <Address>456 Elm St</Address>
         <City>Memphis</City>
         <State>TN</State>
         <Zipcode>38115</Zipcode>
         <Rent>1060</Rent>
         <Dist>.25</Dist>
      </Comp>
      <Comp>
         <Address>987 Oak St</Address>
         <City>Memphis</City>
         <State>TN</State>
         <Zipcode>38115</Zipcode>
         <Rent>925</Rent>
         <Dist>.28</Dist>
      </Comp>
   </Comps>
</rentrangeAPI>

我希望尽可能简洁,让解串器完成工作。

1 个答案:

答案 0 :(得分:0)

试试这个......

Usings ..

using System.IO;
using System.Xml.Serialization;

Classes ....(使用http://xmltocsharp.azurewebsites.net/从yourXML创建)

[XmlRoot(ElementName = "Comp")]
public class Comp
{
    [XmlElement(ElementName = "Address")]
    public string Address { get; set; }
    [XmlElement(ElementName = "City")]
    public string City { get; set; }
    [XmlElement(ElementName = "State")]
    public string State { get; set; }
    [XmlElement(ElementName = "Zipcode")]
    public string Zipcode { get; set; }
    [XmlElement(ElementName = "Rent")]
    public string Rent { get; set; }
    [XmlElement(ElementName = "Dist")]
    public string Dist { get; set; }
}

[XmlRoot(ElementName = "Comps")]
public class Comps
{
    [XmlElement(ElementName = "Comp")]
    public List<Comp> Comp { get; set; }
}

[XmlRoot(ElementName = "rentrangeAPI")]
public class RentrangeAPI
{
    [XmlElement(ElementName = "ErrorCode")]
    public string ErrorCode { get; set; }
    [XmlElement(ElementName = "ErrorInfo")]
    public string ErrorInfo { get; set; }
    [XmlElement(ElementName = "Address")]
    public string Address { get; set; }
    [XmlElement(ElementName = "City")]
    public string City { get; set; }
    [XmlElement(ElementName = "State")]
    public string State { get; set; }
    [XmlElement(ElementName = "Zipcode")]
    public string Zipcode { get; set; }
    [XmlElement(ElementName = "Sqft")]
    public string Sqft { get; set; }
    [XmlElement(ElementName = "Comps")]
    public Comps Comps { get; set; }
}

...代码

        RentrangeAPI dezerializedXML = new RentrangeAPI();
        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(RentrangeAPI));
        using (FileStream stream = File.OpenRead(@"xml.xml"))
        {
            dezerializedXML = (RentrangeAPI)serializer.Deserialize(stream);
        } // Put a break-point here, then mouse-over dezerializedXML

我把你的XML放在一个文件(xml.xml)中并从那里读取....