Solved

Delete from an XML File

Posted on 2006-06-16
2
199 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

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…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

821 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