XML deserialization to Model, XML serialization Model
Don't talk about anything. paste the Code directly.
XML for Reverse Sequence
<? Xml version = '1. 0' encoding = 'utf-8'?> <GetCitiesListResponse> <Result Code = '000000'> success </Result> <CitiesList> <City PinYin = 'zhanjiang 'HasOutService = 'n'> ZHANJIANG </City> <City PinYin = 'hangzhou' HasOutService = 'n'> zhangzhou </City> </CitiesList> </GetCitiesListResponse>
Deserialization 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; } }
Background code is not the core of this topic, so it will not be posted .......
XML deserialization
Create three classes based on the XML you provided, and then use the method on the first floor to reverse the XML file into the MyData class. I can't afford the word "master". I hope more talented people will come...
[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 deserialization Error
The xml file may be incorrectly formatted.
I am talking about the content format. We recommend that you serialize the content once and compare the serialized file with your current xml file to see if the xml format is wrong.