Pergunta

all deserialization works except for the lists:

(resdes is a list)

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

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

    // CONSTRUCTOR
    public SearchResponse()
    {}
}

this works:

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


    public class Return 
{

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

 }

but the list of items doesn't work it deserailizes into 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 class:

 [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()
    { }
}
Foi útil?

Solução

*NOTE: Do not forget to add [DataMember] on each property or member of class *

If you want to do this with DataContract way, use it in the following manner:

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

and

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

rest is same as prev answer by me.

Outras dicas

You need to create two classes as:

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

and

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

Then use the following code to deserialize it, as:

        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);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top