Solved

Delete from an XML File

Posted on 2006-06-16
2
196 Views
Last Modified: 2010-04-23
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
Comment
Question by:sneeri_c
2 Comments
 
LVL 7

Expert Comment

by:pradeepsudharsan
Comment Utility
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
 
LVL 6

Accepted Solution

by:
Nandakumar Sakthivel earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now