Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-02-28
4
Medium Priority
?
990 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
1
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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

1
 

Author Closing Comment

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

Expert Comment

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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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