Everton Elvio Koser
Everton Elvio Koser

Reputation: 123

Transform XmlNode into XmlReader

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
        }
    }
}

Upvotes: 4

Views: 2382

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500805

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...)

Upvotes: 13

Related Questions