convert XmlNode to XNode?

 ̄綄美尐妖づ 提交于 2020-01-01 04:38:05

问题


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


回答1:


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

XmlNode myNode;
XNode translatedNode = XDocument.Parse(myNode.OuterXml);



回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/231323/convert-xmlnode-to-xnode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!