VB.NET中操作xml文件(插入节点、修改、删除)

已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">

<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
</bookstore>

1、往<bookstore>节点中插入一个<book>节点:
Dim xmlDoc As New XmlDocument()
xmlDoc.Load("bookstore.xml")
Dim root As XmlNode = xmlDoc.SelectSingleNode("bookstore") '查找<bookstore>
Dim xe1 As XmlElement = xmlDoc.CreateElement("book") '创建一个<book>节点
xe1.SetAttribute("genre", "李赞红") '设置该节点genre属性
xe1.SetAttribute("ISBN", "2-3631-4") '设置该节点ISBN属性
Dim xesub1 As XmlElement = xmlDoc.CreateElement("title")
xesub1.InnerText = "CS从入门到精通" '设置文本节点
xe1.AppendChild(xesub1) '添加到<book>节点中
Dim xesub2 As XmlElement = xmlDoc.CreateElement("author")
xesub2.InnerText = "候捷"
xe1.AppendChild(xesub2)
Dim xesub3 As XmlElement = xmlDoc.CreateElement("price")
xesub3.InnerText = "58.3"
xe1.AppendChild(xesub3)
root.AppendChild(xe1) '添加到<bookstore>节点中
xmlDoc.Save("bookstore.xml")
//===============================================
结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>候捷</author>
<price>58.3</price>
</book>
</bookstore>

2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
Dim nodeList As XmlNodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes '获取bookstore节点的所有子节点
Dim xn As XmlNode
For Each xn In nodeList '遍历所有子节点
Dim xe As XmlElement = CType(xn, XmlElement) '将子节点类型转换为XmlElement类型
If xe.GetAttribute("genre") = "李赞红" Then '如果genre属性值为“李赞红”
xe.SetAttribute("genre", "update李赞红") '则修改该属性为“update李赞红”
Dim nls As XmlNodeList = xe.ChildNodes '继续获取xe子节点的所有子节点
Dim xn1 As XmlNode
For Each xn1 In nls '遍历
Dim xe2 As XmlElement = CType(xn1, XmlElement) '转换类型
If xe2.Name = "author" Then '如果找到
xe2.InnerText = "亚胜" '则修改
Exit ForEach '找到退出来就可以了
End If
Next xn1
Exit ForEach
End If
Next xn
xmlDoc.Save("bookstore.xml") '保存。
//==================================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book genre="fantasy" ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book genre="update李赞红" ISBN="2-3631-4">
<title>CS从入门到精通</title>
<author>亚胜</author>
<price>58.3</price>
</book>
</bookstore>

3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
Dim xnl As XmlNodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes
Dim xn As XmlNode
For Each xn In xnl
Dim xe As XmlElement = CType(xn, XmlElement)
If xe.GetAttribute("genre") = "fantasy" Then
xe.RemoveAttribute("genre") '删除genre属性
Else
If xe.GetAttribute("genre") = "update李赞红" Then
xe.RemoveAll() '删除该节点的全部内容
End If
End If
Next xn
xmlDoc.Save("bookstore.xml")
//===========================================
最后结果为:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
<book ISBN="2-3631-4">
<title>Oberon's Legacy</title>
<author>Corets, Eva</author>
<price>5.95</price>
</book>
<book>
</book>
</bookstore>

4、显示所有数据。
Dim xn As XmlNode = xmlDoc.SelectSingleNode("bookstore")
Dim xnl As XmlNodeList = xn.ChildNodes
Dim xnf As XmlNode
For Each xnf In xnl
Dim xe As XmlElement = CType(xnf, XmlElement)
Console.WriteLine(xe.GetAttribute("genre")) '显示属性值
Console.WriteLine(xe.GetAttribute("ISBN"))

Dim xnf1 As XmlNodeList = xe.ChildNodes
Dim xn2 As XmlNode
For Each xn2 In xnf1
Console.WriteLine(xn2.InnerText) '显示子节点点文本
Next xn2
Next xnf

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 好的,下面是一个 VB.NET 操作 XML 文件的示例: 1. 创建 XML 文件: ``` Dim xmlDoc As New XmlDocument() Dim xmlDeclaration As XmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", Nothing) xmlDoc.AppendChild(xmlDeclaration) Dim rootNode As XmlElement = xmlDoc.CreateElement("Root") xmlDoc.AppendChild(rootNode) xmlDoc.Save("test.xml") ``` 2. 读取 XML 文件: ``` Dim xmlDoc As New XmlDocument() xmlDoc.Load("test.xml") Dim rootNode As XmlElement = xmlDoc.DocumentElement For Each childNode As XmlNode In rootNode.ChildNodes Console.WriteLine(childNode.Name & ": " & childNode.InnerText) Next ``` 3. 修改 XML 文件: ``` Dim xmlDoc As New XmlDocument() xmlDoc.Load("test.xml") Dim rootNode As XmlElement = xmlDoc.DocumentElement For Each childNode As XmlNode In rootNode.ChildNodes If childNode.Name = "NodeName" Then childNode.InnerText = "New Value" End If Next xmlDoc.Save("test.xml") ``` 希望这些代码对你有所帮助。 ### 回答2: VB.net操作XML文件常用的类是XmlDocument类和XmlNode类。下面是一个使用VB.net操作XML文件的示例。 ```vb.net Imports System.Xml Public Class XMLExample Public Sub ReadXMLFile(filePath As String) ' 创建一个XmlDocument对象 Dim xmlDoc As New XmlDocument() ' 加载XML文件 xmlDoc.Load(filePath) ' 获取根节点 Dim rootNode As XmlNode = xmlDoc.DocumentElement ' 遍历根节点下的所有子节点 For Each childNode As XmlNode In rootNode.ChildNodes ' 输出子节点的名称和内容 Console.WriteLine("节点名称: " & childNode.Name) Console.WriteLine("节点内容: " & childNode.InnerText) Next End Sub Public Sub WriteXMLFile(filePath As String) ' 创建一个XmlDocument对象 Dim xmlDoc As New XmlDocument() ' 创建根节点 Dim rootNode As XmlNode = xmlDoc.CreateElement("Books") ' 将根节点添加到XmlDocument对象 xmlDoc.AppendChild(rootNode) ' 创建子节点 Dim bookNode As XmlNode = xmlDoc.CreateElement("Book") ' 创建子节点的属性 Dim attrib As XmlAttribute = xmlDoc.CreateAttribute("ISBN") attrib.Value = "978-7-121-32712-3" ' 将属性添加到子节点 bookNode.Attributes.Append(attrib) ' 将子节点添加到根节点 rootNode.AppendChild(bookNode) ' 创建子节点的子节点 Dim titleNode As XmlNode = xmlDoc.CreateElement("Title") titleNode.InnerText = "VB.net XML文件操作实例" ' 将子节点的子节点添加到子节点 bookNode.AppendChild(titleNode) ' 保存XML文件 xmlDoc.Save(filePath) End Sub End Class ' 使用示例 Private Sub Main() Dim example As New XMLExample() Dim filePath As String = "example.xml" ' 写入XML文件 example.WriteXMLFile(filePath) ' 读取XML文件 example.ReadXMLFile(filePath) End Sub ``` 上述示例,提供了读取XML文件和写入XML文件的两个方法。创建了一个XmlDocument对象来加载和操作XML文件。通过XmlDocument对象的方法和属性,可以方便地读取和修改XML文件的内容。读取XML文件时,通过遍历节点的方式获取节点的名称和内容。写入XML文件时,通过创建节点和属性的方式构建XML节点树,并将节点添加到XmlDocument对象。最后使用XmlDocument对象的Save方法将XML文件保存到磁盘。 ### 回答3: 在VB.net,我们可以使用System.Xml命名空间下的类来进行XML文件操作。下面是一个XML文件操作的实例: 首先,我们需要在程序引入System.Xml的命名空间,以便使用相应的类。可以在代码文件的顶部添加以下代码: ```vb Imports System.Xml ``` 接下来,我们需要创建一个XmlDocument对象来加载XML文件。假设我们有一个名为"example.xml"的XML文件,它的内容如下: ```xml <?xml version="1.0" encoding="UTF-8"?> <products> <product> <id>1</id> <name>Product1</name> <price>10.0</price> </product> <product> <id>2</id> <name>Product2</name> <price>20.0</price> </product> </products> ``` 我们可以使用以下代码来加载XML文件: ```vb Dim xmlDoc As New XmlDocument() xmlDoc.Load("example.xml") ``` 接下来,我们可以使用SelectNodes或SelectSingleNode方法来选择XML节点。例如,要选择所有的product节点,可以使用以下代码: ```vb Dim productNodes As XmlNodeList = xmlDoc.SelectNodes("/products/product") ``` 如果要选择某个具体的节点,可以使用SelectSingleNode方法,并传入XPath表达式。例如,要选择第一个product节点的name子节点,可以使用以下代码: ```vb Dim nameNode As XmlNode = xmlDoc.SelectSingleNode("/products/product[1]/name") ``` 要访问节点的内容,可以使用InnerText属性或Value属性。例如,要获取第一个product节点的name子节点的文本内容,可以使用以下代码: ```vb Dim name As String = nameNode.InnerText ``` 如果要修改节点的内容,可以直接修改节点的InnerText属性。例如,要将第一个product节点的name子节点的文本内容修改为"New Product",可以使用以下代码: ```vb nameNode.InnerText = "New Product" ``` 最后,我们需要保存修改后的XML文件。可以使用Save方法来保存XML文件。例如,要保存修改后的XML文件为"example_modified.xml",可以使用以下代码: ```vb xmlDoc.Save("example_modified.xml") ``` 以上就是一个简单的VB.net操作XML文件的示例。通过使用System.Xml命名空间下的类,我们可以加载、选择、修改和保存XML文件的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值