Today I found a cool Visual Studio functionality: you can paste an XML or JSON source as Classes, in fact creating all the object model to serialize and deserialize object with the xml format, all this without using xsd.exe tool.
Here’s the very simple steps regarding an XML but it’s the same for JSON:
1 – The most difficult step….. copy the xml source in the clipboard, something like CTRL+A and CTRL+C 🙂
Is ridiculous to add a screenshot, but I’ve got it, so why not!
2 – Create a new empy class file… no more screenshot please! ok here we go 😉
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; namespace ConvertXmlToCSharpClasses { internal class Program { private static void Main(string[] args) { TestSample1(); TestSample2(); Console.WriteLine("Press enter to exit the application..."); Console.ReadLine(); } private static void TestSample1() { var serializer = new XmlSerializer(typeof(library)); var buffer = Encoding.UTF8.GetBytes(Resources.Sample1); using (var stream = new MemoryStream(buffer)) { var library = (library)serializer.Deserialize(stream); library.book.Dump("Book"); library.book.title.Dump("Book Title"); library.book.author.Dump("Book Title"); } } private static void TestSample2() { var serializer = new XmlSerializer(typeof(catalog)); var buffer = Encoding.UTF8.GetBytes(Resources.Sample2); using (var stream = new MemoryStream(buffer)) { var catalog = (catalog)serializer.Deserialize(stream); catalog.product.Dump("Product").catalog_item.Dump("Product Items")[0].size.Dump("Item Size")[0].color_swatch.Dump("Color Swatch"); } } } }
You can also download the test project.
4 – Enjoy your saved time
Reblogged this on Dinesh Ram Kali..
Good to know !!!
Thanks, this was super useful for a large xml document I needed to convert!
Thanks dineshramitc, Nitesh Kumar and Daniel Zoeller for the feedback!
Thanks for the example!
Excellent, saves lot of time.
Thanks Alan & shashank for the feedback 🙂
Thank you a lot. Good to know this.
Thanks for a very good post.
I’ve tried it, and it works for the most, but the xml doc. i have, have only closing tags for empty tags, and this give an exception…. do you have encountered this ? and if so how do you solve it
Can you send a small sample to better understands which is the problem?
Thanks for this post!
Re posted in http://tech.insideroy.com/?p=76
I have and XmlDocument class that contains xml informaiton.
How would I use the classes with the XMLDocument class?
awesome. Thanks Michael
Thanks a lot! very useful…. and screenshots are perfect as it’s not necessary to read everything 😉
Awesome! Thanks
Wow very useful. Was having an issue with an attribute. this resolved it . Thanks much 🙂
best Tools to do that : xsd2code.com
Reblogged this on Mathieu Auclair.