Click here to Skip to main content
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 23:16pm
Omkaara1.1K
Edited 22-May-13 0:11am
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)



Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2014
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