简介:XML的增删改查
开发过程中有许多后台操作XML的过程,每次需要操作时都是找很多代码来参考或者百度一下。今天决定补充下XML操作的知识,把XML操作的增删改查都写了一遍,供以后开发参考
查询:
Public Sub Read()
'获取item节点
Dim xmlNodeList As XmlNodeList = Nothing
xmlNodeList = XmlDoc.SelectNodes("/root/item")
'遍历所有的item节点
Console.WriteLine("遍历item节点")
For Each xmlNode As XmlNode In xmlNodeList
Console.WriteLine("item节点的RoomInfo属性读取:{0}", xmlNode.Attributes("RoomInfo").Value)
Console.WriteLine("item节点的RoomInfo的值读取:{0}", xmlNode.InnerText)
Next '遍历所有items下的item节点
Console.WriteLine("遍历items下的item节点")
xmlNodeList = XmlDoc.SelectNodes("/root/items/item")
For Each xmlNode As XmlNode In xmlNodeList
Console.WriteLine("item节点的RoomInfo属性读取:{0}", xmlNode.Attributes("RoomInfo").Value)
Console.WriteLine("item节点的RoomInfo的值读取:{0}", xmlNode.InnerText)
Next '遍历第一个items下的item节点 (第一种方法适用于有id的)
Console.WriteLine("遍历第一个items下的item节点")
xmlNodeList = XmlDoc.SelectNodes("/root/items[@id='item1']/item")
For Each xmlNode As XmlNode In xmlNodeList
Console.WriteLine("item节点的RoomInfo属性读取:{0}", xmlNode.Attributes("RoomInfo").Value)
Console.WriteLine("item节点的RoomInfo的值读取:{0}", xmlNode.InnerText)
Next '遍历第一个items下的item节点 (第二种方法适用于节点没有特别标识)
'Console.WriteLine("遍历第一个items下的item节点")
'xmlNodeList = XmlDoc.SelectNodes("/root/items")
'Dim xmlNodeList1 As XmlNodeList = xmlNodeList(0).SelectNodes("item")
'For Each xmlNode As XmlNode In xmlNodeList1
' Console.WriteLine("item节点的RoomInfo属性读取:{0}", xmlNode.Attributes("RoomInfo").Value)
' Console.WriteLine("item节点的RoomInfo的值读取:{0}", xmlNode.InnerText)
'Next '遍历第二个items下的Group下的item节点
Console.WriteLine("遍历第二个items下的Group下的item节点")
xmlNodeList = XmlDoc.SelectNodes("/root/items[@id='item2']/Group[@id='g1']/item")
For Each xmlNode As XmlNode In xmlNodeList
Console.WriteLine("item节点的RoomInfo属性读取:{0}", xmlNode.Attributes("RoomInfo").Value)
Console.WriteLine("item节点的RoomInfo的值读取:{0}", xmlNode.InnerText)
Next End Sub
更新:
Public Sub Update()
'修改所有的item节点RoomInfo的属性的值为123
Console.WriteLine("修改所有的item节点RoomInfo的属性的值为123")
Dim xmlNodeList As XmlNodeList = Nothing
xmlNodeList = XmlDoc.SelectNodes("/root/item")
For Each xmlNode As XmlNode In xmlNodeList
xmlNode.Attributes("RoomInfo").Value = ""
Next
'修改所有的item节点的值为123
Console.WriteLine("修改所有的item节点的值为123")
xmlNodeList = XmlDoc.SelectNodes("/root/item")
For Each xmlNode As XmlNode In xmlNodeList
xmlNode.InnerText = ""
Next
Console.WriteLine(XmlDoc.InnerXml)
End Sub
删除:
Public Sub Delete()
'删除所有的item节点
Console.WriteLine("删除所有的item节点")
Dim xmlNodeList As XmlNodeList = Nothing
xmlNodeList = XmlDoc.SelectNodes("/root/item")
For Each xmlNode As XmlNode In xmlNodeList
xmlNode.ParentNode.RemoveChild(xmlNode)
Next
'删除所有的items下的子节点
Console.WriteLine("删除所有的items下的子节点")
xmlNodeList = XmlDoc.SelectNodes("/root/items")
For Each xmlNode As XmlNode In xmlNodeList
xmlNode.RemoveAll()
'xmlNode.ParentNode.RemoveChild(xmlNode)
Next
Console.WriteLine(XmlDoc.InnerXml)
End Sub
添加:
Public Sub Add()
'item节点添加test=123属性
Console.WriteLine("item节点添加test=123属性")
Dim xmlNodeList As XmlNodeList = Nothing
Dim xmlAttribute As XmlAttribute = Nothing
xmlNodeList = XmlDoc.SelectNodes("/root/item")
For Each xmlNode As XmlNode In xmlNodeList
'属性的名称
xmlAttribute = XmlDoc.CreateAttribute("test")
xmlAttribute.Value = ""
xmlNode.Attributes.Append(xmlAttribute)
Next
'item节点添加子节点<test name="123">
Console.WriteLine("item节点添加子节点<test name='123'>")
xmlNodeList = XmlDoc.SelectNodes("/root/item")
Dim xmlNewNode As XmlElement = Nothing
For Each xmlNode As XmlNode In xmlNodeList
xmlNewNode = XmlDoc.CreateElement("test")
xmlNewNode.SetAttribute("name", "")
xmlNewNode.InnerText = ""
xmlNode.AppendChild(xmlNewNode)
Next
XmlDoc.Save("C:\Users\dudj\Desktop\123.xml")
Console.WriteLine(XmlDoc.InnerXml)
End Sub