什麼也不多說,直接貼代碼
需要反序列的XML
<?xml version='1.0' encoding='utf-8' ?> <GetCitiesListResponse> <Result Code='000000'>成功</Result> <CitiesList> <City PinYin='ZHANJIANG' HasOutService='N'>湛江</City> <City PinYin='zhangzhou' HasOutService='N'>漳州</City> </CitiesList> </GetCitiesListResponse>
反序列化Model:
[XmlRoot("GetCitiesListResponse")] public class GetCitiesListResponse { public Result Result { get; set; } [XmlArray("CitiesList"), XmlArrayItem("City")] public City[] CitiesList { get; set; } } public class Result { [XmlAttribute("Code")] public string Code { get; set; } [XmlText] public string Value { get; set; } } [XmlRoot("City")] public class City { [XmlAttribute("PinYin")] public string PinYin { get; set; } [XmlAttribute("HasOutService")] public string HasOutService { get; set; } [XmlText] public string Value { get; set; } }
後台代碼不算本話題的核心,就不貼出來了。。。。。。。
根據你提供的XML,創建三個類,然後再利用一樓的方法把這個XML文件反序化成MyData類。高手這兩個字我受不起,希望有更牛X的人來...
[Serializable]
public class something
{
pubilc int id {get;set;}
public string name{get;set;}
}
[Serializable]
public class MyFile
{
[xmlAttribute("name")]
pubilc string name{get;set;}
[xmlAttribute("size")]
pulic string size{get;set;}
[xmlElement("file")]
public List<something> listSomething{get;set;}
}
[Serializable]
[xmlRoot("data")]
pubilc class MyData
{
[xmlArray("files")]
[xmlArrayItem(file)]
public List<MyFile>ListMyfile{get;set;}
}
xml文件有問題,可能是文件格式不正確
我說的是內容的格式,建議你序列化一次,把序列化後的文件和你現在的xml文件比較一下,就知道xml格式那錯了