5

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

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.