【问题标题】:Deserailizing xml to C# list is empty?将 xml 脱轨到 C# 列表是空的?
【发布时间】:2014-02-07 12:01:58
【问题描述】:

除列表之外的所有反序列化工作:

(resdes 是一个列表)

public class SearchResponse
{
    // ELEMENTS
    [XmlElement("ResDes")]
    public ResDes resdes { get; set; }

    [XmlElement("Return")]
    public Return returns { get; set; }

    // CONSTRUCTOR
    public SearchResponse()
    {}
}

这行得通:

 <Return>
  <test>0010000725</test> 
  </Return>


    public class Return 
{

    // ELEMENTS
    [XmlElement("test")]
    public Test test{ get; set; }

 }

但项目列表不起作用,它会变为 null

<ResDes >
 <item>  
     <PoBox />    
     <City1>South Korea</City1>      
     <Country>SK</Country>    
 </item>   
</ResDes >


 public class ResDes 
{
    // ELEMENTS
    [XmlArrayItem("item")]
    public List<ResDesItem> ResDesItem { get; set; }

    // CONSTRUCTOR
    public ResDes ()
    { }
}

Resdesitem 类:

 [DataContract()]
public class ResDesItem
{

    [XmlElement("PoBox")]
    public PoBox PoBox { get; set; }

    [XmlElement("City1")]
    public City1 City1 { get; set; }


    [XmlElement("Country")]
    public EtResultDetAdritemCountry EtResultDetAdritemCountry { get; set; }

    // CONSTRUCTOR
    public ResDesItem()
    { }
}

【问题讨论】:

  • 您是否尝试使用 [Serializable] 属性标记 ResDesItem 类?
  • 嗨,我目前正在为手机平台开发,所以我使用了它的等价物[DataContract()],但它仍然是空的
  • ResDesItem 类在哪里?你能粘贴那个类吗?

标签: c# xml deserialization poco


【解决方案1】:

*注意:不要忘记在每个属性或类 * 的成员上添加 [DataMember]

如果你想用 DataContract 的方式做到这一点,请按以下方式使用:

[DataContract]
[XmlRoot("item")]
[XmlType]
public class ResDesItem
{
    [XmlElement("PoBox")]
    [DataMember]
    public string PoBox { get; set; }
    [XmlElement("City1")]
    [DataMember]
    public string City1 { get; set; }
    [XmlElement("Country")]
    [DataMember]
    public string Country { get; set; }
}

[DataContract]
[XmlRoot("ResDes")]
[XmlType]
public class ResDes
{
    [XmlElement("item")]
    [DataMember]
    public List<ResDesItem> ResDesItem { get; set; }
}

其余的和我之前的回答一样。

【讨论】:

  • 你为什么回答两次?
  • 如果有人想使用 [DataContract] 进行序列化,第二个答案将起作用,如果有人想要使用 [Serializable] 答案 1 将起作用。所以,我已经为它添加了单独的答案。
  • @JohnSaunders,我需要删除任何答案吗?
  • 嗨,我确实尝试过这段代码,但是我遇到了异常,因为我想反序列化所有 xml,所以将其更改为流并尝试反序列化响应肥皂头会出现异常,但是我猜你答案将起作用,谢谢您的帮助
【解决方案2】:

你需要创建两个类:

[Serializable]
[XmlRoot("ResDes")]
[XmlType]
public class ResDes
{
    [XmlElement("item")]
    public List<ResDesItem> ResDesItem { get; set; }
}

[Serializable]
[XmlRoot("item")]
[XmlType]
public class ResDesItem
{
    [XmlElement("PoBox")]
    public string PoBox { get; set; }
    [XmlElement("City1")]
    public string City1 { get; set; }
    [XmlElement("Country")]
    public string Country { get; set; }
}

然后使用下面的代码对其进行反序列化,如:

        XmlSerializer xmlReq = new XmlSerializer(typeof(ResDes));
        string xml = @"<ResDes> <item><PoBox/><City1>South Korea</City1><Country>SK</Country> </item></ResDes>";

        Stream stream = new MemoryStream(Encoding.Unicode.GetBytes(xml));
        var resposnseXml = (ResDes)xmlReq.Deserialize(stream);

【讨论】:

  • 嗨 Mahesh,我确实尝试了上面的代码,但是我不得不使用 DataContract,而不是使用 Serializable 属性,因为它仅在 windows phone 中可用,但仍然不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多