Solved

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

Posted on 2014-02-28
4
964 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

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

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 …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now