如何使用 vb.net 在 xml 文件的特定位置添加 xmlnode

发布于 2024-08-16 08:31:31 字数 830 浏览 3 评论 0原文

有人可以帮助我使用 vb.net 将节点写入现有 xml 文件的特定位置吗?

<xml>
  <person>
    <name>a</name>
  </person>
  <person>
    <name>b</name>
  </person>
  <person>
    <name>c</name>
  </person>
  <person>
    <name>d</name>
  </person>
</xml>

在这里,我想在节点 person 之后插入一个节点,其中包含节点 name 的值 a

<xml>
  <person>
    <name>a</name>
  </person>
  <person>
    <name>e</name>
  </person>
  <person>
    <name>b</name>
  </person>
  <person>
  <name>c</name>
  </person>
  <person>
    <name>d</name>
  </person>
</xml>

could anybody help me to write nodes to existing xml file to a particular position using vb.net?

<xml>
  <person>
    <name>a</name>
  </person>
  <person>
    <name>b</name>
  </person>
  <person>
    <name>c</name>
  </person>
  <person>
    <name>d</name>
  </person>
</xml>

here i want to insert a node just after the node person which contains value a for the node name.

<xml>
  <person>
    <name>a</name>
  </person>
  <person>
    <name>e</name>
  </person>
  <person>
    <name>b</name>
  </person>
  <person>
  <name>c</name>
  </person>
  <person>
    <name>d</name>
  </person>
</xml>

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

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

发布评论

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

评论(2

云仙小弟 2024-08-23 08:31:31

~|简单节点添加

要向 XML 文件添加新节点,XmlNode 类提供了各种方法。首先,XML 文件必须有一个根。这确保该文件至少有一个节点。在添加新节点之前,您必须有另一个节点的引用。此信息将允许您决定将新节点放置在哪里。

要将新节点添加为现有节点的子节点,最简单的使用位置是将新节点添加到现有节点的节点列表的末尾。 XmlNode.AppendChild() 方法支持此位置。其语法为:

Public Overridable Function AppendChild(ByVal newChild As XmlNode) As XmlNode

该方法接受将要创建的新节点作为参数。这意味着您可以首先“构建”一个 XmlNode 对象。为此,您可以使用指向要创建的节点类型的指针。|~

复制自 这个

~|Simple Node Addition

To add a new node to an XML file, the XmlNode class provides various methods. To start, an XML file must have a root. This ensures that the file has at least one node. Before adding a new node, you must have a reference of another node. This information will allow you to decide where to position the new node.

To add a new node as a child of an existing node, the simplest position to use is to add the new node at the end of the list of nodes of the existing node. This position is supported by the XmlNode.AppendChild() method. Its syntax is:

Public Overridable Function AppendChild(ByVal newChild As XmlNode) As XmlNode

This method accepts as argument the new node that will be created. This means that you can first "build" an XmlNode object. To do this, you can use a pointer to the type of node you want to create.|~

copied from this

仅此而已 2024-08-23 08:31:31

我遇到了与您相同的问题,我认为这是不可能的(谷歌搜索尚未产生一些好的结果),但我可能会向我的节点添加一个属性,然后使用 Jon Skeet 的建议

I got the same problem as you have, I do not think it is possible (google search have yet to yield some good results), but I will probably add an attribute to my nodes and then sort it using Jon Skeet's suggestion.

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