如何使用 vb.net 或 C# 为 xmlDocument 中的一组节点提供/添加父节点 [英] How to give/add parent node for set of nodes in xmlDocument using vb.net or C#
本文介绍了如何使用 vb.net 或 C# 为 xmlDocument 中的一组节点提供/添加父节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 vb.net 为 xmlDocument 中的节点集添加或提供父节点.
How to add or give the parent node for set of nodes in xmlDocument using vb.net.
我有以下 xml 节点
I am having following xml nodes
<books>
<title>title</title>
<isbn>123456</isbn>
<surname>surname</surname>
<givenname>givenname</givenname>
</books>
现在我想为
和
添加父节点
如下.>
Now i want to add parent node <author>
for <surname>
and <givenname>
as follows.
<books>
<title>title</title>
<isbn>123456</isbn>
<author>
<surname>surname</surname>
<givenname>givenname</givenname>
</author>
</books>
谁能告诉我如何在vb.net中的xmlDocument中做到这一点.
can any one tell me how to do it in xmlDocument in vb.net.
推荐答案
您需要:
- 获取要修改的父节点(书籍).
- 添加新的子元素(作者).
- 获取要移动的子元素(姓氏和名字).
- 对于要移动的每个节点,将其从其父节点(书籍)中删除,然后将其作为子节点添加到新的父节点(作者).
例如:
Dim doc As New XmlDocument()
doc.Load(xmlFilePath)
Dim bookToModify As XmlNode = doc.SelectSingleNode("/books")
Dim author As XmlNode = doc.CreateElement("author")
bookToModify.AppendChild(author)
For Each node As XmlNode In bookToModify.SelectNodes("surname | givenname")
node.ParentNode.RemoveChild(node)
author.AppendChild(node)
Next
这篇关于如何使用 vb.net 或 C# 为 xmlDocument 中的一组节点提供/添加父节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文