Try Dim xmlDoc As XmlDocument =New XmlDocument
xmlDoc.Load("myTest.xml")
Dim NodeList As XmlNodeList = xmlDoc.SelectSingleNode("//Root").ChildNodes
'下面是查Code是否唯一 '后有自动增加Code的值的代码. ForEach xn As XmlNode In NodeList
Dim myxe As XmlElement = xn '转换类型 If myxe.GetAttribute("Code") = Filtrate(strCode) Then ReturnFalse Exit Function EndIf Next Dim root As XmlNode = xmlDoc.SelectSingleNode("Root") '查找<Root> Dim xe As XmlElement = xmlDoc.CreateElement("Profile") '创建一个<Profile>节点 xe.SetAttribute("Code", Filtrate(strCode)) '设置该节点Code属性 xe.SetAttribute("Name", Filtrate(strName)) '设置该节点的Name属性 xe.SetAttribute("City", Filtrate(strCity)) '设置该节点的City属性 '以下是在<Profile>下建子节点<"myTest"> 'XmlElement xesub1=xmlDoc.CreateElement("myTest"); 'xesub1.InnerText="myText"; '设置文本节点 'xe1.AppendChild(xesub1); '添加到<Root>节点中 root.AppendChild(xe) '添加到<Root>节点中 xmlDoc.Save("myTest.xml")
ReturnTrue Catch ex As Exception
ReturnFalse EndTry End Function
DataSet
Try Dim doc As XmlDocument =New XmlDocument
doc.Load("myTest.xml")
Dim node As XmlNode = doc.SelectSingleNode("//Root")
Dim read As StringReader =New StringReader(node.OuterXml)
Dim myds As DataSet =New DataSet
myds.ReadXml(read)
Return myds
IfNot myds IsNothingThen myds.Dispose()
Catch ex As Exception
' MsgBox(ex.Message) EndTry End Function
三、搜索数据,读某一条数据也可以用这直接读。
DataSet
Try Dim doc As XmlDocument =New XmlDocument
doc.Load("myTest.xml")
Dim NodeList As XmlNodeList = doc.SelectSingleNode("//Root").ChildNodes
Dim i AsInteger Dim strXml AsString="<myx_Search>"'把搜索到的放在这里 ForEach xn As XmlNode In NodeList
If strKeyText =""Or strKeyText ="全部"Then'搜索所有的属性值 For i =0To xn.Attributes.Count -1 Dim xe As XmlElement = xn '转换类型 If xe.GetAttribute(xn.Attributes(i).Name) = strKeyWord Then strXml = strXml & xn.OuterXml
EndIf Next Else Dim xe As XmlElement = xn '转换类型 If xe.GetAttribute(strKeyText) = strKeyWord Then'搜索指定的属性值 strXml = strXml & xn.OuterXml
EndIf EndIf Next strXml = strXml &"</myx_Search>" 'MsgBox(strXml) Dim read As StringReader =New StringReader(strXml)
Dim myds As DataSet =New DataSet
myds.ReadXml(read)
Return myds
IfNot myds IsNothingThen myds.Dispose()
Catch ex As Exception
MsgBox(ex.Message)
EndTry End Function
四、修改
Try Dim xmlDoc AsNew XmlDocument
xmlDoc.Load("myTest.xml")
Dim xnl As XmlNodeList = xmlDoc.SelectSingleNode("//Root").ChildNodes
ForEach xn As XmlNode In xnl
Dim xe As XmlElement = xn '转换类型 If xe.GetAttribute("Code") = strCode Then 'xe.SetAttribute("Code", strCode) 'xe.InnerText="" xe.SetAttribute("Name", strName)
xe.SetAttribute("City", strCity)
EndIf Next xmlDoc.Save("myTest.xml")
ReturnTrue Catch ex As Exception
ReturnFalse EndTry End Function
五、删除
Try Dim xmlDoc AsNew XmlDocument
xmlDoc.Load("myTest.xml")
Dim sxn As XmlNode = xmlDoc.DocumentElement.S _
electSingleNode("//Root/Profile[@Code="& strCode &"]")
xmlDoc.SelectSingleNode("//Root").RemoveChild(sxn)
'Dim xn As XmlNode = xmlDoc.SelectSingleNode("//ProFile/Vendor[@Code=" & strCode & "]") ''For Each xn As XmlNode In xnl '' Dim xe As XmlElement = xn '' If xe.GetAttribute("Code") = strCode Then '' ' xe.RemoveAll() '删除该节点的全部内容 '' ' xe.RemoveAttribute("Name") 删除Name属性 '' End If ''Next xmlDoc.Save("myTest.xml")
ReturnTrue Catch ex As Exception
MsgBox(ex.Message)
ReturnFalse EndTry End Function