[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Delete from an XML File

I am using VB.NET 2005.

I want to delete from this XML File. If I have the following:

<?xml version="1.0" encoding="utf-8" ?>
<applicationlist>
     <app>
          <name>AC</name>
          <srcdir>c:\AC</srcdir>
      </app>
     <app>
          <name>NewAppInfo</name>
          <srcdir>c:\Info</srcdir>
      </app>
</applicationlist>

I want to delete one of the <app> sections of the xml. So I then have:

<?xml version="1.0" encoding="utf-8" ?>
<applicationlist>
     <app>
          <name>AC</name>
          <srcdir>c:\AC</srcdir>
     </app>
</applicationlist>

Thanks
0
sneeri_c
Asked:
sneeri_c
1 Solution
 
pradeepsudharsanCommented:
Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" & _
                    "<title>Pride And Prejudice</title>" & _
                    "</book>")
       
        Dim root As XmlNode = doc.DocumentElement
       
        'Remove the title element.
        root.RemoveChild(root.FirstChild)
       
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub

0
 
Nandakumar SakthivelTechnology LeadCommented:
Hi,
    you can also try this also
 
            Dim xmldoc As New XmlDocument
            Dim childElements As XmlNodeList
            Dim childNode As XmlNode
            xmldoc.Load(<filename>)
            childNode = xmldoc.SelectSingleNode("//applicationlist/app[name='AC']")
            childNode.ParentNode.RemoveChild(childNode)
            xmldoc.Save(<filename>)

In this using the xpath query you can select the node based on some value(eg..delete the node which has name = 'ac')
The above code will delete the node which has the name value as 'ac'
make sure that your file is not read only before running this code

Thanks,
Nanda
 
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now