开发者

how to add a xmlnode in a particular postion of xml file using vb.net

开发者 https://www.devze.com 2022-12-15 15:08 出处:网络
could anybod开发者_如何转开发y help me to write nodes to existing xml file to a particular position using vb.net?

could anybod开发者_如何转开发y 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>


~|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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号