Remove a node from an XML File using VB.NET
-
8 апреля 2008 г. 9:47
Hai
i ve an xml file in the format
<
Root><ID>A</ID>
<ID>B</ID>
<ID>C</ID>
</Root>
I want to delete the node whose ID is "B" ... how can i do this ??
The code that i wrote is
Dim xpathquery as String
Dim doc as new XmlDocument
Dim selectedNode,main_node as XmlNode
doc.Load(filnam)
xpathquery = "/Root[ID='" & txtID.Text & "']"
selectedNode = doc.SelectSingleNode(xpathquery)main_node = doc.SelectSingleNode(xpathquery)
doc.DocumentElement.RemoveChild(main_node)
doc.Save(filnam)
when i tried executing this code i m getting the error "the node to be removed is not a child of this node " ... how can i correct this error ?? Can anybody help me ??
Any help appreciated ...
Thanx in Advance
Все ответы
-
8 апреля 2008 г. 11:27
Your XPath expression is not correct, if I understand you correctly that you want to remove the 'ID' element.
Here is a sample:
Code SnippetDim doc As New XmlDocument()doc.Load(
"file.xml") Dim node As XmlNode = doc.SelectSingleNode("Root/ID[. = 'B']") If node IsNot Nothing Thennode.ParentNode.RemoveChild(node)
' Save change here or later e.g.
doc.Save("file.xml")
End If -
13 апреля 2012 г. 9:45I have the same problem. I have converted my XML into a table by VB. it has the ability to add new data into the table and it will also added to my XML. But I want to add the function for removing a row of data which are coming from my XML. but I don't know how can I modify your code to do this? can you help me?

