Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Thanks in advance
i want to Delete a node from XMl file using VB.net

Here is my Xml file


XML
<?xml version='1.0'?>
<Names>
  <Name>
    <First_Name>FN 1</First_Name>
    <Middle_Name>MN 1</Middle_Name>
    <Last_Name>LN 1</Last_Name>
    <Country_Code>001</Country_Code>
    <Area_Code></Area_Code>
    <Phone_No></Phone_No>
    <Email_Address>mail@email.com</Email_Address>
    <Address>Address 1</Address>
  </Name>
  <Name>
    <First_Name>FN 2</First_Name>
    <Middle_Name>MN 2</Middle_Name>
    <Last_Name>LN 2</Last_Name>
    <Country_Code>002</Country_Code>
    <Area_Code></Area_Code>
    <Phone_No></Phone_No>
    <Email_Address></Email_Address>
    <Address>Address 2</Address>
  </Name>
  </Names>


and the code for deleting is

VB
Dim xd As New XmlDocument()
        xd.Load(App_Path)
        Dim nod As XmlNode = xd.SelectSingleNode("//First_Name[. = 'FN 1']" &        
        "/parent::node()/First_Name")
        If nod IsNot Nothing Then
            'nod.ParentNode.RemoveChild(nod)
            nod.ParentNode.RemoveAll()
            xd.Save(App_Path)
            ctlStatus.Text = "Deleted"

        End If



Problem i am facing is its deleting all child rows like this

XML
<Name>
  </Name>


i want to delete whole node means including Parent node <name> also
any help suggestion
Posted

1 solution

Perhaps something like:
VB
nod.ParentNode.ParentNode.RemoveChild(nod.ParentNode)
 
Share this answer
 

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