您可以在 C# 中使用 XPath 将 xml 文档过滤到节点的子集吗? [英] Can you filter an xml document to a subset of nodes using XPath in C#?

查看:21
本文介绍了您可以在 C# 中使用 XPath 将 xml 文档过滤到节点的子集吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 XPath 将 Xml 文档过滤为自身的子集.

I'm trying to filter an Xml document so into a subset of itself using XPath.

我已使用 XPath 获取 XmlNodeList,但我需要将其转换为 XML 文档.

I have used XPath get an XmlNodeList, but I need to transform this into an XML document.

有没有办法将 XMLNodeList 转换为 XmlDocument 或通过直接过滤另一个 XmlDocument 来生成 XmlDocument?

Is there a way to either transform an XMLNodeList into an XmlDocument or to produce an XmlDocument by filtering another XmlDocument directly?

推荐答案

使用 XmlDocument,您需要将这些节点导入到第二个文档中;

With XmlDocument, you will need to import those nodes into a second document;

    XmlDocument doc = new XmlDocument();
    XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("root"));
    XmlNodeList list = // your query
    foreach (XmlElement child in list)
    {
        root.AppendChild(doc.ImportNode(child, true));
    }

这篇关于您可以在 C# 中使用 XPath 将 xml 文档过滤到节点的子集吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆