• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 7584
  • Last Modified:

How can I convert a XMlNode To XMlDocument

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.
  • 2
1 Solution
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

>  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

subhorachanaAuthor Commented:
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.
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now