Help with removing data element

Posted on 2014-08-02
Last Modified: 2014-08-02

How do you remove a data element from a xml file using VB.NET?


Question by:vcharles
    LVL 21

    Expert Comment

    by:Randy Poole
    Dim doc As New XmlDocument()
    Dim n As XmlNode = doc.SelectSingleNode("xpath expression")
    If n IsNot Nothing Then
    End If
    set doc=nothing

    Open in new window

    replace "file.xml" with the name of the file you want to alter, and change the "xpath expression" to select the node you would like to remove
    LVL 62

    Accepted Solution

    Hi Victor;

    You did not specify whether you want to remove a single node or multiple nodes with the same node name. In the below code snippet lets say you have multiple nodes with the name Item and wish to remove them all just change NODENAME in the code with Item and it should do it for you. The code can be easily modify to remove a single node.

    Dim inputXML As String = "File path and name"
    Dim saveXML As String = "File path and name"
    '' Load the XML document
    Dim xdoc = XDocument.Load(inputXML)
    '' Replace the "NODENAME"
    Dim results As List(Of XElement) = (From n In xdoc.Descendants("NODENAME") _
                                        Select n).ToList()
    For Each node As XElement In results 

    Open in new window


    Author Closing Comment

    Thank You.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    729 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

    20 Experts available now in Live!

    Get 1:1 Help Now