Click here to Skip to main content
11,705,998 members (59,307 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB8.0 XML VB.NET
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
Posted 21-May-13 22:16pm
Omkaara1.2K
Edited 21-May-13 23:11pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Omkaara at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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]
  Permalink  
v2
Comments
Omkaara at 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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 208
1 Sergey Alexandrovich Kryukov 142
2 Inimicos 60
3 ppolymorphe 57
4 ProgramFOX 50
0 OriginalGriff 9,037
1 Sergey Alexandrovich Kryukov 8,417
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,626


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100