Solved

Delete from an XML File

Posted on 2006-06-16
2
197 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
ID: 16920611
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
ID: 16925711
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

12 Experts available now in Live!

Get 1:1 Help Now