1

I'd like to delete a whole "TableData" Elemt with a specific ID (ID 10 in this case) from my xml:

<DataPaths>
  <TableData>
    <ID>10</ID>
    <TablePath>C:\Users\Tom\Test.xls</TablePath>
    <TableName>TestName</TableName>
    <Mode>TestMode</Mode>
  </TableData>
</DataPaths>

I tried two different solutions and I think I'm almost there.

Solution 1:

Dim XmldocDel As New XmlDocument()
        XmldocDel.Load("" & buildSettingsPath)
        Dim node As XmlNode
        node = XmldocDel.SelectSingleNode("//DataPaths/TableData[ID = '10']")

        If MessageBox.Show("Are you sure you want to delete:", MessageBoxButtons.YesNo) = DialogResult.Yes Then
            If node IsNot Nothing Then
            node.ParentNode.RemoveChild(node)
            'node.ParentNode.RemoveAll()

            XmldocDel.Save("" & buildSettingsPath)
            Reload()
        Else
            MessageBox.Show("No node found")
        End If

        Else
            Exit Sub
        End If

I'm getting a null Exception here.

Solution 2:

Dim clientNodes = XmldocDel.SelectNodes("//ID")
        For Each elem As Xml.XmlElement In clientNodes
            If elem.InnerText = "10" Then
                elem.ParentNode.RemoveChild(elem)
                Exit For
            End If
        Next

This will simply do nothing.

1 Answer 1

3

Using solution 1, since you want to delete the whole TableData element, the XPath should've selected TableData element instead of ID element :

.....
node = XmldocDel.SelectSingleNode("//DataPaths/TableData[ID = '10']")
.....

*) updated above XPath a bit. Looking at the XML posted, the first step should be DataPaths instead of DataSetPaths.

UPDATE :

Your actual XML has default namespace. So in this case, you need to register a prefix mapped to the default namespace URI using XmlNamespaceManager. Use the registered prefix in the XPath, and pass the namespace manager as 2nd parameter of SelectSingleNode() method :

Dim nsManager As New XmlNamespaceManager(New NameTable())
nsManager.AddNamespace("d", "http://tempuri.org/DataPaths.xsd")
.....
node = XmldocDel.SelectSingleNode("//d:DataPaths/d:TableData[d:ID = '10']", nsManager)
.....

[.NET fiddle demo]

Sign up to request clarification or add additional context in comments.

6 Comments

@TomWayne This code will certainly not result in a null exception. Your unchecked use of node one line later however would. Your fault, never use variables that can be null without checking if they are valid.
Was a pasting mistake by myself (the path should be correct) see updated question. Now it gives me the MessageBox "No node found"
@TomWayne Your actual XML must have significant difference from the one posted here (I suspect it has default namespace?). The code should work against XML posted here, see the demo : dotnetfiddle.net/ZTvcIT
xmlns="tempuri.org/DataPaths.xsd" is the default namespace. check updated answer for one possible solution
updated comment: @har07 I also think so....even this code will select nothing: 'node = XmldocDel.SelectSingleNode("/DataPaths/TableData[1]/ID")' the xml begins with: '<?xml version="1.0" standalone="yes"?> <DataPaths xmlns="tempuri.org/DataPaths.xsd">' I also have to admit that I am completly new to VB and working with XML Data. I never understood why the namespace only works with that content. Do I even have to use the namespace?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.