如何在 XML 中更深入地导航并在其中附加数据

发布于 2024-07-14 00:56:26 字数 734 浏览 4 评论 0原文

我已将 XmlDocument 加载到内存中并创建了新的 XmlElement。 现在我尝试将 XmlElement 添加到路径 /report/section/hosts 但我不知道如何。 我可以轻松地将其添加到 XML 的根节点下方,但我不知道如何在 XML 中导航更深层次并仅附加到那里。 在伪中,我试图执行此操作:

doc.SelectNodes("/report/section/hosts").AppendChild(subRoot);

代码:

        XmlDocument doc = new XmlDocument();

        doc.Load("c:\\data.xml");

        //host
        XmlElement subRoot = doc.CreateElement("host");

        //Name
        XmlElement ElName = doc.CreateElement("name");
        XmlText TxtName = doc.CreateTextNode("text text");
        ElName.AppendChild(TxtName);
        subRoot.AppendChild(ElName);
        doc.DocumentElement.AppendChild(subRoot);

        doc.Save("c:\\data.xml");

I have loaded XmlDocument into memory and created new XmlElement. Now I am trying to add XmlElement to the path /report/section/hosts but I don't know how. I can add it easily below root node of XML but I cannot figure out how can I navigate deeper level in XML and just append there. In pseudo I am trying to do this:

doc.SelectNodes("/report/section/hosts").AppendChild(subRoot);

The code:

        XmlDocument doc = new XmlDocument();

        doc.Load("c:\\data.xml");

        //host
        XmlElement subRoot = doc.CreateElement("host");

        //Name
        XmlElement ElName = doc.CreateElement("name");
        XmlText TxtName = doc.CreateTextNode("text text");
        ElName.AppendChild(TxtName);
        subRoot.AppendChild(ElName);
        doc.DocumentElement.AppendChild(subRoot);

        doc.Save("c:\\data.xml");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫九 2024-07-21 00:56:26

尝试 SelectSingleNode 而不是 SelectNodes

XmlElement parent = (XmlElement)doc.SelectSingleNode("/report/section/hosts")
parent.AppendChild(subRoot);

Try SelectSingleNode instead of SelectNodes

XmlElement parent = (XmlElement)doc.SelectSingleNode("/report/section/hosts")
parent.AppendChild(subRoot);
身边 2024-07-21 00:56:26

你快到了。 尝试使用 SelectSingleNode 代替:

XmlNode node = doc.SelectSingleNode("/report/section/hosts");
node.AppendChild(subRoot);

You are almost there. Try using SelectSingleNode instead:

XmlNode node = doc.SelectSingleNode("/report/section/hosts");
node.AppendChild(subRoot);
清风无影 2024-07-21 00:56:26

SelectNodes 方法返回节点列表。
您应该使用 SelectSingleNode 代替...

例如(我的头顶,没有在 Visual Studio 中测试)

doc.SelectSingleNode("/report/section/hosts").AppendChild(subRoot);

The SelectNodes method returns a list of Nodes.
You should use SelectSingleNode instead...

e.g. (top of my head, did not test in Visual Studio)

doc.SelectSingleNode("/report/section/hosts").AppendChild(subRoot);
动次打次papapa 2024-07-21 00:56:26

您需要在文档中获取对 XmlElement 的引用(而不是根)以追加到该元素。 XmlDocument 上有许多方法,例如例如 GetElementByIdSelectSingleNode 它们以不同的方式为您执行此操作,请研究品味。

也就是说,这个领域的整个 API 通常被认为有点痛苦,你们有可用的 LINQ 吗?

You need to get a reference to an XmlElement in your doc (other than the root) to append to. There are a number of methods available on XmlDocument such as GetElementById and SelectSingleNode which do this for you in different ways, research to taste.

That said, the whole API in this area is generally regarded as a bit painful, do you have LINQ available?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文