Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XmlDocument object into an XmlNode object - C#?

How do I convert an XmlDocument to a XmlNode in C#? I need to send the entire XmlDocument object to as an input parameter to a .NET web service.

like image 485
Michael Kniskern Avatar asked Mar 04 '10 21:03

Michael Kniskern


1 Answers

A XmlDocument is a XmlNode, so you can just pass the document object.

Or you could send its DocumentElement, or any Node returned from an XPath query.

XmlDocument doc = null;
XmlNode node = doc;

XmlNode node = doc.DocumentElement;

XmlNode node = doc.SelectSingleNode("/foo/bar");

No casting or converting is needed unless you need to disambiguate XmlNode from XmlDocument for a method with overloads for both parameter types. If this is the case, use either of the cast or as operators.

like image 171
Lachlan Roche Avatar answered Sep 27 '22 02:09

Lachlan Roche