[VS tips] automatically generate Types Based on XML

Source: Internet
Author: User

A function is added to the VS version corresponding to. NET 4.5 (do not ask which version I want to use). It is very practical. You can generate a new type based on the XML document. This function is available in the Edit> select Paste menu of. How to play? Don't worry. Let's do it.

Taking the RSS source of Netease news center as an example, URI must point to the XML document. I used the content of the "Cultural Information" channel for testing. The URI is as follows:

Http://book.163.com/special/0092451H/rss_whzx.xml

 

Enter the above URI in the address bar of the browser, open the RSS source, and view the source. Select the entire XML document by selecting all.

Return to the VS Project (note that you must first create a project), create a code file, and then place the cursor to the place where you want to insert the new class, then, choose Edit> select Paste> paste XML as a class ].

Then, we will see a magic scene. The generated code is as follows:

    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]    public partial class rss    {        ……/// <remarks/>        public rssChannel channel        {            get            {                return this.channelField;            }            set            {                this.channelField = value;            }        }        /// <remarks/>        [System.Xml.Serialization.XmlAttributeAttribute()]        public decimal version        {            get            {                return this.versionField;            }            set            {                this.versionField = value;            }        }    }    /// <remarks/>    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class rssChannel    {        ……/// <remarks/>        public string title        {            get            {                return this.titleField;            }            set            {                this.titleField = value;            }        }        /// <remarks/>        public string link        {            get            {                return this.linkField;            }            set            {                this.linkField = value;            }        }        ……        }        /// <remarks/>        [System.Xml.Serialization.XmlElementAttribute("item")]        public rssChannelItem[] item        {            get            {                return this.itemField;            }            set            {                this.itemField = value;            }        }    }    /// <remarks/>    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class rssChannelItem    {        ……/// <remarks/>        public string title        {            get            {                return this.titleField;            }            set            {                this.titleField = value;            }        }……/// <remarks/>        public string pubDate        {            get            {                return this.pubDateField;            }            set            {                this.pubDateField = value;            }        }        /// <remarks/>        public rssChannelItemGuid guid        {            get            {                return this.guidField;            }            set            {                this.guidField = value;            }        }    }    /// <remarks/>    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]    public partial class rssChannelItemGuid    {        private bool isPermaLinkField;        private string valueField;        /// <remarks/>        [System.Xml.Serialization.XmlAttributeAttribute()]        public bool isPermaLink        {            get            {                return this.isPermaLinkField;            }            set            {                this.isPermaLinkField = value;            }        }        /// <remarks/>        [System.Xml.Serialization.XmlTextAttribute()]        public string Value        {            get            {                return this.valueField;            }            set            {                this.valueField = value;            }        }    }

 

OK, the code is generated, and you will know how to do it later.

Here, I will give you an example to get the XML document of the RSS source online through code, and then get an example of the rss class just generated through XML deserialization, then it is used just like accessing other common types.

Static void Main (string [] args) {// set the buffer size of the Console window Console. SetBufferSize (Console. largest1_wwidth, 1000); // obtain the xml uri string uri =" http://book.163.com/special/0092451H/rss_whzx.xml "; WebClient wc = new WebClient (); // get the RSS content byte [] xmlData = wc. downloadData (uri); rss wy_rss = null; using (MemoryStream MS = new MemoryStream (xmlData) {// deserialization XmlSerializer xs = new XmlSerializer (typeof (rss )); wy_rss = (rss) xs. deserialize (MS);} // if deserialization is successful, the related content if (wy_rss! = Null) {Console. writeLine ("version: {0}", wy_rss.version); rssChannel channel = wy_rss.channel; Console. writeLine ("channel name: {0}", channel. title); Console. writeLine ("channel Description: \ n {0} \ n", channel. description); Console. writeLine ("========= resource list ========"); foreach (rssChannelItem item in channel. item) {Console. writeLine ("title: {0}", item. title); Console. writeLine ("Description: {0}", item. description); Console. writeLine ("link: {0}", item. link); Console. writeLine ("Release Date: {0}", item. pubDate); Console. writeLine ("---------------------------------") ;}} Console. read ();}

The final result is shown in.

 

How can this function be used?

 

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.