XML deserialization to Model, XML serialization Model

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.