Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform XmlNode into XmlReader

Tags:

c#

xml

I wonder what is the best way to turn a XmlNode object into an XmlReader... I could even name a few ways to do this ... But they use a MemoryStream to make the transformation.

XmlNode content = // My data
using (System.IO.MemoryStream mm = new System.IO.MemoryStream())
{
    using (System.Xml.XmlWriter wtr = System.Xml.XmlWriter.Create(mm))
    {
        content.WriteTo(wtr);
        wtr.Flush();
        mm.Position = 0;
        using (System.Xml.XmlReader reader = System.Xml.XmlReader.Create(mm))
        {
            // Here I have the object
        }
    }
}
like image 832
Everton Elvio Koser Avatar asked Dec 25 '22 21:12

Everton Elvio Koser


1 Answers

Just use the XmlNodeReader constructor:

using (XmlReader reader = new XmlNodeReader(content))
{
    // ...
}

(The documentation says you should use XmlReader.Create - but there are no overloads taking an XmlNode, so that doesn't seem terribly useful to me...)

like image 200
Jon Skeet Avatar answered Jan 01 '23 20:01

Jon Skeet