XML 和 .NET:如何用从原始 xml 数据加载的许多其他节点替换特定节点

发布于 2024-08-30 00:46:27 字数 494 浏览 6 评论 0原文

假设主 xml 文件中有这样一个元素:。第二个文件 gr1.xml 包含如下内容:

<item name="i1">Item one</item>
<item name="i2">Item two</item>
<item name="i3">Item three</item>

请注意,gr1.xml 中没有 XML 声明,只是没有单个父节点的普通项目。

那么...用 替换 的最佳方式是什么?

我已经尝试过一些方法,例如使用 XML 声明手动将 gr1.xml 内容封装到单个节点中并将其加载到 XmlDocument 中,但这看起来不是一个好的解决方案。

Let's suppose we have an element like this one in the main xml file: <group name="gr1" filename="groups/gr1.xml"/>. The second file gr1.xml contains something like this:

<item name="i1">Item one</item>
<item name="i2">Item two</item>
<item name="i3">Item three</item>

Note that there is no XML declaration in gr1.xml, just plain items without single parent node.

So… Which is the best way to replace <group/> with its <item/>s?

I have already tried some things like manual enclosing of gr1.xml content into a single node with XML declaration and loading it into XmlDocument, but it doesn't look like a good solution.

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

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

发布评论

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

评论(1

长发绾君心 2024-09-06 00:46:27

您可以使用 DTD 和外部实体。加载外部文件时,您需要显式启用 DTD 处理。

否则,您可以加载文档片段,并自己添加其节点。

You could use a DTD and external entity. You would need to explicitly enable DTD processing when loading the outer file.

Otherwise you can load a document fragment, and add its nodes yourself.

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