Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to join on ID, with prefix? 15 60
Call Controller Action Method from ASPX 2 15
JSON  parse help 7 27
SQL Server Connection String through a VPN 8 28
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

803 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