使用vb.net删除xml节点 [英] delete xml node using vb.net
本文介绍了使用vb.net删除xml节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的xml文件...
<? xml version = 1.0 独立 = 是 ? >
< DsXmlDbDemo xmlns = http://tempuri.org/DsXmlDbDemo.xsd >
< 客户 >
< CustomerId > 1 < / CustomerId >
< 名称 > dfgdfg < /姓名 >
< Address1 > dfgdfg < / Address1 >
< Address2 > dfgdfg < / Address2 >
< 城市 > dfgdfgdf < / City >
< St > dfgdfg < / st >
< Zip > 45646 < / Zip >
< / Customers >
< 客户 >
< CustomerId > 2 < / CustomerId >
< 名称 > asfdasd < /姓名 >
< 地址1 > sedufsufh < / Address1 >
< 地址2 > sdoifo < / Address2 >
< 城市 > efef < / City >
< St > sdfsf < / st >
< Zip > 6656 < / Zip >
< /客户 >
< / DsXmlDbDemo >
i想要删除customerid = 2的完整节点
i尝试下面的代码
intid = dgvxml( 0 ,dgvxml.CurrentRow.Index).Value.ToString
' 如果intid = 0那么
Dim xd As 新 XmlDocument()
xd.Load(mXmlFilePath)
' 否则
Dim 节点 As XmlNode
< span class =code-keyword> Dim xe As XmlElement = xd .DocumentElement
while x。阅读
如果 x.Name = CustomerId 然后
node = xd .SelectSingleNode( / Customers [CustomerId ='2'])
xd.ParentNode .RemoveChild(node)
xd.Save(mXmlFilePath)
退出 while
结束 如果
结束 虽然
i am getting
node = nothing
请帮助
解决方案
类似于什么这里建议可能会ork:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/781d901a-2647-4fc0-9ed2-a7328992a2fb [ ^ ]
请参考以下内容:
从XML文档中删除节点,内容和值 [ ^ ]
从XML树中删除元素,属性和节点 [ ^ ]
修改XML文档中的节点,内容和值 [ ^ ]
根据需要更改以下代码...
Dim xmlDoc As Xml.XmlDocument = Nothing ,xmlRoot As Xml.XmlNode = Nothing
Dim xmlNode As Xml.XmlNode = Nothing ,xmlChild As Xml.XmlNode = Nothing
尝试
xmlDoc = < span class =code-keyword>新 Xml.XmlDocument()
xmlDoc.Lo ad( E:\ MyXML.xml)
xmlRoot = xmlDoc.DocumentElement( )
xmlNode = xmlRoot.ChildNodes( 1 )
对于 每个 xmlChild 在 xmlNode
MsgBox(xmlChild.Name& vbCr& xmlChild.InnerText)
下一步
Catch ex As 异常
MsgBox(ex.Message,MsgBoxStyle.Exclamation, 错误)
最后
xmlRoot = 什么
xmlNode = 没什么
xmlDoc = 没什么
< span class =code-keyword>结束 尝试
[/ EDIT]
below is my xml file...
<?xml version="1.0" standalone="yes"?>
<DsXmlDbDemo xmlns="http://tempuri.org/DsXmlDbDemo.xsd">
<Customers>
<CustomerId>1</CustomerId>
<Name>dfgdfg</Name>
<Address1>dfgdfg</Address1>
<Address2>dfgdfg</Address2>
<City>dfgdfgdf</City>
<St>dfgdfg</St>
<Zip>45646</Zip>
</Customers>
<Customers>
<CustomerId>2</CustomerId>
<Name>asfdasd</Name>
<Address1>sedufsufh</Address1>
<Address2>sdoifo</Address2>
<City>efef</City>
<St>sdfsf</St>
<Zip>6656</Zip>
</Customers>
</DsXmlDbDemo>
i want to delete the full node where customerid=2
i have tried below code
intid = dgvxml(0, dgvxml.CurrentRow.Index).Value.ToString
'If intid = 0 Then
Dim xd As New XmlDocument()
xd.Load(mXmlFilePath)
'Else
Dim node As XmlNode
Dim xe As XmlElement = xd.DocumentElement
While x.Read
If x.Name = "CustomerId" Then
node = xd.SelectSingleNode("/Customers[CustomerId='2']")
xd.ParentNode.RemoveChild(node)
xd.Save(mXmlFilePath)
Exit While
End If
End While
i am getting
node = nothing
please help
解决方案
Something like what is suggested here might work:
http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/781d901a-2647-4fc0-9ed2-a7328992a2fb[^]
Please, refer these:
Removing Nodes, Content, and Values from an XML Document [^]
Removing Elements, Attributes, and Nodes from an XML Tree[^]
Modifying Nodes, Content, and Values in an XML Document[^]
[EDIT #1]
Change below code to your needs...
Dim xmlDoc As Xml.XmlDocument = Nothing, xmlRoot As Xml.XmlNode = Nothing Dim xmlNode As Xml.XmlNode = Nothing, xmlChild As Xml.XmlNode = Nothing Try xmlDoc = New Xml.XmlDocument() xmlDoc.Load("E:\MyXML.xml") xmlRoot = xmlDoc.DocumentElement() xmlNode = xmlRoot.ChildNodes(1) For Each xmlChild In xmlNode MsgBox(xmlChild.Name & vbCr & xmlChild.InnerText) Next Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Error") Finally xmlRoot = Nothing xmlNode = Nothing xmlDoc = Nothing End Try
[/EDIT]
这篇关于使用vb.net删除xml节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文