XMLSerializer未正确地为List类型的属性反序列化XML

时间:2012-07-18 15:00:32

标签: c# xml-serialization

我得到一个XML类型的数据,比如这个;

<Response>
  <Clients>
    <Client>
      <ID>1</ID>
      <Name>John</Name>
      <Age>25</Age>
      <Address>Some address</Address>
    </Client>
    <Client>
      <ID>2</ID>
      <Name>Mark</Name>
      <Age>22</Age>
      <Address>Some address2</Address>
    </Client>
    <Client>
      <ID>3</ID>
      <Name>Phil</Name>
      <Age>30</Age>
      <Address>Some address3</Address>
    </Client>
  </Clients>
</Response>

在C#中,我有以下代码:

[XmlRoot("Response")]
public class MyClients
{
    [XmlElement("Clients", typeof(MyClient))]
    public List<MyClient> Clients { get; set; }
}

public class MyClient
{
    [XmlElement("ID")]
    public int ID;

    [XmlElement("Name")]
    public string Name;

    [XmlElement("Age")]
    public int Age;

    [XmlElement("Address")]
    public string Address;
}

我尝试使用

获取此数据
public ActionResult GetClients()
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("someUrl");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    XmlSerializer serializer = new XmlSerializer(typeof(WFMClientsList));
    Stream receiveStream = response.GetResponseStream();
    WFMClientsList clients = (MyClients)serializer.Deserialize(receiveStream);
}

但我没有得到回应。

任何人都可以解释如何正确地将XML反序列化为List<MyClient>吗?

3 个答案:

答案 0 :(得分:5)

问题在于您声明的内容实际上与您的XML不匹配。如果从当前声明序列化对象,则得到:

<?xml version="1.0" encoding="utf-16"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Clients>
    <ID>1</ID>
    <Name>John</Name>
    <Age>25</Age>
    <Address>Some address</Address>
  </Clients>
</Response>

尝试:

[XmlRoot("Response")]
public class MyClients
{
    [XmlArray("Clients")]
    [XmlArrayItem("Client")]
    public List<MyClient> Clients { get; set; }
}

[XmlRoot("Client")]
public class MyClient
{
    [XmlElement("ID")]
    public int ID;
    [XmlElement("Name")]
    public string Name;
    [XmlElement("Age")]
    public int Age;
    [XmlElement("Address")]
    public string Address;
}

产生:

<?xml version="1.0" encoding="utf-16"?>
<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Clients>
    <Client>
      <ID>1</ID>
      <Name>John</Name>
      <Age>25</Age>
      <Address>Some address</Address>
    </Client>
  </Clients>
</Response>

答案 1 :(得分:1)

这是错误的XmlSerializer serializer = new XmlSerializer(typeof(WFMClientsList)); 必须是XmlSerializer serializer = new XmlSerializer(typeof(MyClients));

你失去了班级MyClient的属性

答案 2 :(得分:0)

  public static T DeserializeObject<T>(string xml)
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
            return (T)xs.Deserialize(memoryStream);
        }


private static Byte[] StringToUTF8ByteArray(string pXmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            byte[] byteArray = encoding.GetBytes(pXmlString);
            return byteArray;
        }