Solved

Help with Removing Data element from xml file using VB.NET

Posted on 2014-02-28
4
981 Views
Last Modified: 2014-03-01
Hi,

How do I remove the entire record from my xml file if the table is missing a data element?

For example:

If file2.xml  contains

<Root>
<Table2>
<RIC>ItemA<RIC>
<NOP>ItemA</NOP>
<FIF>ItemC<FIF>
</Table2>
<Table2>
<RIC>ItemA<RIC>
<NOP>ItemA</NOP>
<FIF>ItemC<FIF>
</Table2>
<Table2>
<RIC>ItemA<RIC>
<NOP>ItemA</NOP>
</Table2>
</Root>

How do I remove the last record because <FIF> is missing.

Thanks,

Victor
0
Comment
Question by:vcharles
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Author Comment

by:vcharles
ID: 39896512
Hi,

I used the code below to remove records by selecting IDs, how I search for if nodes does not exist and if yes remove the record?


 Dim dkA As New XmlDocument
                dkA.Load(Application.StartupPath & "\App_Data\LinkA.xml")
                Dim pizA As XmlElement = dkA.DocumentElement
                For Each topping As Xml.XmlNode In piz.SelectNodes("/Root/LinkA[LinkAID='" & Link_ID & "']")
                    topping.ParentNode.RemoveChild(topping)
                    dkA.Save(Application.StartupPath & "\App_Data\LinkA.xml")
                    Exit For
                Next
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39896659
Hi Victor;

Try this code snippet it should give the wanted results.

' Load the document to be updated
Dim fileXdoc2 = XDocument.Load("C:\Working Directory\file2.xml")

' Get a list of nodes that are missing one or more elements
Dim missingElements = From f2 In fileXdoc2.Descendants("Table2") _
                    Where f2.Element("RIC") Is Nothing OrElse _
                          f2.Element("NOP") Is Nothing OrElse _
                          f2.Element("FIF") Is Nothing _
                    Select f2

' Check to see if any nodes need to be removed
If missingElements IsNot Nothing Then
    ' Remove the missing nodes from the document fileXdoc2
    missingElements.Remove()
End If

fileXdoc2.Save("C:\Working Directory\file2Updated.xml")

Open in new window

0
 

Author Closing Comment

by:vcharles
ID: 39897169
Thank You!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39897288
Glad to see it worked for you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 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