roger.james
roger.james

Reputation: 1498

Obtaining XmlElement from XDocument

I have to pass a whole XML document into a 3rd party function. The parameter is XmlElement.

To do this until now, I've successfully been using this:

XmlDocument doc;
//doc = ...
XmlElement root = doc.DocumentElement;
3rdPartyFunction(root);

But now I'm using XDocument instead of XmlDocument:

XDocument doc;
//doc = ...
//how to call 3rdPartyFunction?

How do I call the function in this case? Can I convert from "Xml" to "X"?

Upvotes: 1

Views: 2029

Answers (2)

mousio
mousio

Reputation: 10337

[Updated]

XmlDocument xmldoc = new XmlDocument();
using (XmlReader reader = xdoc.CreateReader())
{
    xmldoc.Load(reader);
}
XmlElement root = xmldoc.DocumentElement;
3rdPartyFunction(root);

Upvotes: 2

Adrian Trifan
Adrian Trifan

Reputation: 250

Use this:

var newDoc = new XmlDocument();
newDoc.LoadXml(doc.ToString());
3rdPartyFunction(newDoc);

Upvotes: 5

Related Questions