Shortest way to deserialize XmlDocument
Asked Answered
C

3

19

I am looking for a clean and short way to deserialize a XmlDocument object. The closest thing I found was this but I am really wondering if there is no nicer way to do this (in .NET 4.5 or even 4.6) since I already have the XmlDocument.

So currently this looks as follows:

// aciResponse.Data is a XmlDocument
MyClass response;
using (XmlReader reader = XmlReader.Create((new StringReader(aciResponse.Data.InnerXml))))
{
    var serializer = new XmlSerializer(typeof(MyClass));
    response =  (MyClass)serializer.Deserialize(reader);
}

Thanks for any better idea!

Crisis answered 2/2, 2015 at 17:30 Comment(4)
Can you clarify exactly what you are thinking might constitute a nicer / cleaner way?Niemi
well, something that maybe does not involve to create/open two different readers and using the InnerXml (string)?Crisis
This is what methods and extension methods are for, if you're doing a lot of the same code, extract to a function... That code looks pretty concise to me; so probably be more specific.Lathing
Maybe there really is no "nicer" solution - but that's what I like to find out here ;-)Crisis
V
21

You could forgo the XmlReader and use a TextReader instead and use the TextReader XmlSerializer.Deserialize Method overload.

Working example:

void Main()
{
   String aciResponseData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><bar>test</bar></tag>";
   using(TextReader sr = new StringReader(aciResponseData))
   {
        var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
        MyClass response =  (MyClass)serializer.Deserialize(sr);
        Console.WriteLine(response.bar);
   }
}

[System.Xml.Serialization.XmlRoot("tag")]
public class MyClass
{
   public String bar;
}
Viator answered 2/2, 2015 at 18:18 Comment(1)
Well, that's at least a bit shorter solution :) I'll wait, however, if there might be more suggestions.Crisis
F
24

If you already have a XmlDocument object than you could use XmlNodeReader

MyClass response = null;
XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
using (XmlReader reader = new XmlNodeReader(aciResponse.Data))
{
    response = (MyClass)serializer.Deserialize(reader);
}
Fowl answered 18/1, 2017 at 9:5 Comment(1)
In my case, I found XmlNodeReader ~5 times faster than using XmlReader + StringReaderCoth
V
21

You could forgo the XmlReader and use a TextReader instead and use the TextReader XmlSerializer.Deserialize Method overload.

Working example:

void Main()
{
   String aciResponseData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><bar>test</bar></tag>";
   using(TextReader sr = new StringReader(aciResponseData))
   {
        var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyClass));
        MyClass response =  (MyClass)serializer.Deserialize(sr);
        Console.WriteLine(response.bar);
   }
}

[System.Xml.Serialization.XmlRoot("tag")]
public class MyClass
{
   public String bar;
}
Viator answered 2/2, 2015 at 18:18 Comment(1)
Well, that's at least a bit shorter solution :) I'll wait, however, if there might be more suggestions.Crisis
P
13

There is a better and lazy way to do this. But it is possible only if you are using Visual Studio.

Steps:

  1. Open Visual Studio and create a new class file (say Sample1.cs). Now remove the sample class definition from this class file.
  2. Copy your XML file into the clipboard (Ctrl+A, Ctrl+C)
  3. In Visual Studio, go to Edit menu and select "Paste Special"->"Paste XML as Classes".

Done. Visual Studio will generate all the class definitions required to de-serialize this XML.

Pilarpilaster answered 8/11, 2017 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.