1

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"?

roger.james
  • 1,478
  • 1
  • 11
  • 23
  • Why don't you just read it in as an `XmlElement` in the first place? – Jeff Mercado Jul 27 '13 at 21:58
  • @JeffMercado I don't control the 3rd party function and can't change it. – roger.james Jul 27 '13 at 22:01
  • @roger.james Deleted my post since the suggestion became irrelevant.In this case you [would look @ this post](http://stackoverflow.com/questions/1508572/converting-xdocument-to-xmldocument-and-vice-versa) for transformation. – Mechanical Object Jul 27 '13 at 22:07
  • You misunderstood my question, but my question misunderstood your question. :) I missed the first part of your question. – Jeff Mercado Jul 27 '13 at 22:07

2 Answers2

5

Use this:

var newDoc = new XmlDocument();
newDoc.LoadXml(doc.ToString());
3rdPartyFunction(newDoc);
Adrian Trifan
  • 250
  • 1
  • 5
2

[Updated]

XmlDocument xmldoc = new XmlDocument();
using (XmlReader reader = xdoc.CreateReader())
{
    xmldoc.Load(reader);
}
XmlElement root = xmldoc.DocumentElement;
3rdPartyFunction(root);
mousio
  • 10,079
  • 4
  • 34
  • 43