简体   繁体   English

如何将 XmlNode 转换为 XElement?

[英]How to convert XmlNode into XElement?

I have an old XmlNode -based code.我有一个旧的基于XmlNode的代码。 but the simplest way to solve my current task is to use XElement and LINQ-to-XML.但解决我当前任务的最简单方法是使用XElement和 LINQ-to-XML。 The only problem is that there is no direct or obvious method for converting a XmlNode to a XElement in .NET Framework.唯一的问题是在 .NET Framework 中没有直接或明显的方法将XmlNode转换为XElement

So for starters, I want to implement a method that receives a XmlNode instance and converts it to a XElement instance.所以对于初学者来说,我想实现一个接收XmlNode实例并将其转换为XElement实例的方法。

How can I implement this conversion?如何实现这种转换?

var xElem = XElement.Load( xmlElement.CreateNavigator().ReadSubtree() );

There are two problems with xmlElement.InnerXml used in other answer,其他答案中使用的xmlElement.InnerXml有两个问题,

1- You will loose the root element (Of course, it can be handled easily) 1-您将失去元素(当然,它可以轻松处理)

XmlDocument doc = new XmlDocument();
doc.LoadXml("<root> <sub>aaa</sub> </root>");
var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml);

xElem2 will be <sub>aaa</sub> , without( root ) xElem2将是<sub>aaa</sub> ,没有( root

2- You will get exception if your xml contains text nodes 2-如果您的 xml 包含文本节点,您将收到异常

XmlDocument doc = new XmlDocument();
doc.LoadXml("<root> text <sub>aaa</sub> </root>");
var xElem1 = XElement.Load(doc.DocumentElement.CreateNavigator().ReadSubtree());
var xElem2 = XElement.Parse(doc.DocumentElement.InnerXml); //<-- XmlException

You can try using InnerXml property of XmlElement to get xml content of your element then parse it to XElement usingXElement.Parse :您可以尝试使用InnerXml财产XmlElement让你的元素的XML内容,然后将其解析到XElement使用XElement.Parse

public static XElement ToXELement(this XmlElement source)
{
    return XElement.Parse(source.InnerXml);
}

The only way to take over all is to use OuterXml.接管所有的唯一方法是使用 OuterXml。

XElement.Parse(xNode.OuterXml);

Another way is to change the outer root element via.另一种方法是通过更改外部根元素。

XElement.Parse("<NewRoot>" + xNode.InnerXml + "</NewRoot>");

just use it: XElement e = XElement .Load(node.CreateReader());只需使用它: XElement e = XElement .Load(node.CreateReader());

I hope that helps.我希望这有帮助。

Example:例子:

A real code example The image code above一个真实的代码示例上面的图片代码

public static XNode GetNodeByFilter(XNode node,ref SortedList filter, int position = 1) {公共静态 XNode GetNodeByFilter(XNode 节点,参考 SortedList 过滤器,int 位置 = 1){

XNode result = null; XNode 结果 = null;

 if (filter.TryGetValue(position, out XMLSearchCriteria criteria)) { while (node != null) { XElement e = XElement.Load(node.CreateReader()); if (e.Name.LocalName.Equals(criteria.Node) && CheckIfAllAttributesMatch(e.Attributes(), criteria.Attributes)) { if (++position <= filter.Count) { result = GetNodeByFilter(e.FirstNode, ref filter, position); break; } else { result = node; } } node = node.NextNode; } } return result; }

There actually is a very straightforward way to convert an XNode to an XElement:实际上有一种非常简单的方法可以将 XNode 转换为 XElement:

static XElement ToXElement( XNode node)
{
    return node as XElement; // returns null if node is not an XElement
}

If you are 100% certain the node is an XElement (or you are prepared to deal with the exception if it is not, then you can simply cast: (XElement)node .如果您 100% 确定该节点是 XElement(或者如果不是,您准备处理异常,那么您可以简单地(XElement)node(XElement)node

据我所知,你可以这样做:

XElement xdoc = new XElement(node.Name, node.InnerXml);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM