Generate data type classes from XML
.NET Framework 4.5 includes a new feature to generate data type classes from XML. This article describes how to automatically generate data types for the .NET Blog RSS feed.
Obtain the XML from the .NET Blog RSS feed
In a browser, navigate to the .NET Blog RSS feed.
Copy the text of the feed by pressing Ctrl+A to select all text, and Ctrl+C to copy.
Create the data types
Open a code file where the proxy is to be used. This file should be part of a .NET Framework 4.5 or later project.
Place the cursor in a location in the file outside any existing classes.
Select Edit > Paste Special > Paste XML as Classes.
Classes called
link
,rss
,rssChannel
,rssChannelImage
,rssChannelItem
, andrssChannelItemGuid
are created with the necessary members for accessing the elements in the RSS feed.
Use the generated classes
Once the classes are generated, you can use them in code like any other classes. The following code example returns a new instance of the rssChannelImage
class.
var channelImage = new rssChannelImage()
{
title = "MyImage",
link = "http://www.contoso.com/images/channelImage.jpg",
url = "http://www.contoso.com/entries/myEntry.html"
};