Xmlserializerfactory is pretty cool

Source: Internet
Author: User

The xmlserializerfactory is a new class in. NET 2.0 that provides a factory pattern over the xmlserializer. it maintains the cache of generated assemblies so that you can avoid the xmlserializer leaks. usage doesn' t impact your code much at all.

Here is a quick demo that I threw together. I pulled up my blog's RSS feed and saved it into my Visual Studio project as "xmlfile1.xml ". I then created a schema from it (in Visual Studio, use the "XML/create schema" menu item ). using the schema, I created a serializable class from that schema. this is simple using the command line tools:

Xsd.exe/classes xmlfile1.xsd xmlfile11.xsd xmlfile12.xsd xmlfile13.xsd

The result was a set of XML serializable classes that are already decorated with the appropriate attributes for serialization. The last step is to use the serializable classes with the xmlserializer.

Public RSS getfeed (URI location) {xmlserializerfactory factory = new xmlserializerfactory (); xmlserializer SER = factory. createserializer (typeof (RSS); httpwebrequest request = (httpwebrequest) webrequest. create (location); httpwebresponse response = (httpwebresponse) request. getresponse (); stream responsestream = response. getresponsestream (); RSS feed = (RSS) Ser. deserialize (responsestream); responsestream. close (); Return Feed ;}

A bonus is that in. NET 2.0, the generated types will have public properties by default, which enables rapid UI development using data binding. I created a Windows form with a grid and used the new databinding features to bind to the generated XML serializable classes, using the objectdatasource control. the result is that my UI code is drastically forced CED to create a simple grid UI that displays the data:


RSS feed = getfeed (New uri ("http://blogs.msdn.com/kaevans/rss.aspx");
This. datagridview1.datasource = feed. channel. item;
This. TEXT = feed. channel. title + "-" + feed. channel. description;

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.