Click here to Skip to main content
15,887,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is my xml file...

XML
<?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
VB
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
Posted
Updated 21-May-13 23:11pm
v2

 
Share this answer
 
Comments
Omkaara 23-May-13 5:08am    
For Each myNode As XmlNode In xd.DocumentElement.ChildNodes
' MsgBox(myNode.LocalName.ToString)
If myNode.Attributes.ItemOf("Cid", uri).Value.ToString = intid Then

' myNode.ParentNode.ParentNode.RemoveChild((myNode.ParentNode))

myNode.ParentNode.RemoveChild(myNode)

End If
xd.Save(mXmlFilePath)
Next
i have changed this code now its deleting first node every time
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...
VB
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]
 
Share this answer
 
v2
Comments
Omkaara 23-May-13 5:06am    
For Each myNode As XmlNode In xd.DocumentElement.ChildNodes
' MsgBox(myNode.LocalName.ToString)
If myNode.Attributes.ItemOf("Cid", uri).Value.ToString = intid Then

' myNode.ParentNode.ParentNode.RemoveChild((myNode.ParentNode))

myNode.ParentNode.RemoveChild(myNode)

End If
xd.Save(mXmlFilePath)
Next
i have changed this code now its deleting first node every time
Maciej Los 23-May-13 5:34am    
If any solution was helpful, please, mark this solution as "solved" (green button) - formally.
I'm glad that you have found a solution ;)
Omkaara 24-May-13 8:24am    
i havent found the soln yet
i have mentioned the error in the commnet
every time first node gets deleted not the which i want
Maciej Los 24-May-13 15:47pm    
Please, see my answer after update.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900