我得到一个 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);
}
但我没有得到任何回应。
谁能解释如何List<MyClient>
正确反序列化 XML?