Convert XML to C# Classes

      No Comments on Convert XML to C# Classes

There is a cool Visual Studio functionality (Visual Studio 2012/2013 onward): you can paste an XML source as Classes, in fact creating all the object model to serialize and deserialize object with the xml format, all this without using any tool.

My task was to integrate BOM Weather data ( to one of our sites.

Here’s the very simple steps:

1 – copy the xml source in the clipboard, something like CTRL+A and CTRL+C 🙂

2 – Create a new empty class file

3 – Go to Edit -> Paste Special -> Paste XML As Classes, to paste the generated classes based on the source xml

Here’s the code I’ve used to test the deserialization:

using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using ConsoleDump;
using ConvertXmlToCSharpClasses.Properties;
using System.Xml;

namespace ConvertXmlToCSharpClasses
internal class Program
private static void Main(string[] args)
Console.WriteLine(“Press enter to exit the application…”);
private static void TestSample3()
XmlDocument doc = new XmlDocument();

var serializer = new XmlSerializer(typeof(product));
var buffer = Encoding.UTF8.GetBytes(doc.OuterXml);
using (var stream = new MemoryStream(buffer))
var product = (product)serializer.Deserialize(stream);
foreach(var item in product.forecast) {

//do whatever you want with data

Done! you can modify class and code according to your need.


Leave a Reply

Your email address will not be published. Required fields are marked *