How can I convert a XMlNode To XMlDocument

Posted on 2009-04-20
Last Modified: 2012-05-06
My webservices returns to me the output in XMlNode format. I want to convert it to XMlDocument so that I can traverse through It.
Please let me know how I can convert XMLNode to XMLDocument.
Question by:subhorachana
    LVL 10

    Expert Comment

    Create an XML Document in code
    Dim xmldoc as XmlDocument = new XmlDocument()

    Then use the LoadXML method to load the xml from your XMLNode.


    - Anthony

    LVL 39

    Accepted Solution

    >  I want to convert it to XMlDocument so that I can traverse through It.
     you can also traverse through an XmlNode. You do not need to change it into an XmlDocument. But, if the node is an element then you can transform it into an XmlDocument the following way (note the step ImportNode, that's the key here):

    //for illustration, lets create an "alien" document
    // (i know you have an XmlNode to start with, not a doc)
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<bookstore>" +
                "<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>" +
    // your node (to illustrate where you come from)
    XmlNode node = doc.SelectSingleNode("//book");
    // new document
    XmlDocument docNew = new XmlDocument();
    // we must use ImportNode, that's the key to your conversion
    XmlNode newNode = docNew.ImportNode(node, true);
    // now you can set it as the document element's first child
    // this only works if it is an element, otherwise you 
    // need to create a root node first
    // you can now access your new document
    textBox1.Text = docNew.OuterXml;

    Open in new window


    Author Comment

    You have said that I can traverse through a XMLNode.COuld you please suggest how I can do that.

    ELse a way to convert the XMLNode to datatable will be perfect.
    LVL 39

    Expert Comment

    sorry, I just spend a lot of time showing you in detail how to change form XmlNode to an XmlDocument. Please stick to one question at the time.

    To answer the first question though: you can walk through an XmlNode exactly the same way you would walk through an XmlDocument. You have access to properties like .FirstChild, SelectNodes, Parent etc.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now