views:

1835

answers:

3

Anyone know off the top of their heads how to convert a System.Xml.XmlNode to System.Xml.Linq.XNode?

A: 

I don't think there is, but why would you need to? Each is the lowest 'leaf' of the Xml structure for different ways of reading the document.

If you use Linq to Xml and XDocument you'll have all the linq-style syntax and new functionality, but really all that's about selecting a node.

Once you have the element that you're dealing with, why do you need to switch?

Keith
Just had some old code which is returning an XmlNode but I want to query this node, which appears to actually be a document.
Dave
+2  A: 

I've never tried, but my first thought would be something like:

XmlNode myNode;
XNode translatedNode = XDocument.Parse(myNode.OuterXml);
Chris Shaffer
+4  A: 

Eric White's blog is the place to be for cool XML/XLINQ conversions and such. I know this question pre-date's the post but I found it while looking at some other Q, so maybe people still come across this a fair amount. His blog has plenty of optimized LINQ, like I suspect the .Parse() call for the origional responce is non-optimal, well in-fact I know it is not.

Parse is going to require that the XML be loaded up in one shot, Eric used extension methods which process the XML conversion with XmlReader/Writer's. Those methods are able to stream the input, so if your XML is of any substantional size, you have to use them.

RandomNickName42