Solved

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

Posted on 2014-02-28
4
952 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
  • 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 62

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 62

Expert Comment

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

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

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 …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

757 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

23 Experts available now in Live!

Get 1:1 Help Now