Solved

Help with removing multiple data elements  from xm file

Posted on 2015-01-27
4
77 Views
Last Modified: 2015-01-28
Hi,

How do you modify the code below two remove data elements listed in string variable S if already exist? I am receiving error message "Parent not found"


xml file:

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <NewDataSet>
    <ID>2470</ID>
    <COUNTRY>USA</COUNTRY>
    <NSC>31102</NSC>
    <F1 />
    <F2 />
    <F3 />
    <F4 />
    <F5 />
  </NewDataSet>
</Root>
'' List of new nodes to be added to a document
        Dim S As String = "F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20"
        '' Convert the string to a List(Of String)
        Dim SList As New List(Of String)()
        SList = S.Split(",").ToList()

        '' Load the Document 
        Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP40XYZGG.xml")
        '' Get a reference to the AOP5 nodes for new nodes to be added to
        Dim results = (From n In xdoc.Root.Descendants("NewDataSet")
                       Select n).ToList()


        For Each node As XElement In results
            For Each newNode As String In SList
                node.Remove()  ****Error Parent not found
            Next
        Next

        '' Asve the resulting document
        xdoc.Save(Application.StartupPath + "\AOP40XYZGGA.xml")

Open in new window


Thanks,

Victor
0
Comment
Question by:vcharles
  • 2
  • 2
4 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi Victor;

This modified code should do what you need as I understood from the original code.

'' List of new nodes to be Removed from the document
'' Do not leave a space character between elements because the space becomes
'' part of the element and will NOT match the element you are looking for
Dim S As String = "F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20"
'' Convert the string to a List(Of String)
Dim SList As New List(Of String)()
SList = S.Split(",").ToList()
'' Store the elements to be removed in this list
Dim RemoveList As New List(Of XElement)()

'' Load the Document 
Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP40XYZGG.xml")

'' Get a reference to the NewDataSet nodes as the parent of the XElement to be removed
Dim results = (From n In xdoc.Root.Descendants("NewDataSet")
               Select n).ToList()

'' Iterate thrugh the NewDataSet nodes and add to the RemoveList the nodes that are in the list
For Each node As XElement In results.Descendants()
    If SList.Contains(node.Name.LocalName) Then
        RemoveList.Add(node)
    End If
Next

'' Renove the elements from the document
RemoveList.ForEach(Sub(n) n.Remove())
'' Save the resulting document
xdoc.Save(Application.StartupPath + "\AOP40XYZGGA.xml")

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Hi Fernando,

I will try it and get back to you.

Thanks,

V.
0
 

Author Closing Comment

by:vcharles
Comment Utility
Thank You.
0
 
LVL 62

Expert Comment

by:Fernando Soto
Comment Utility
Not a problem Victor, glad to help.
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

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

8 Experts available now in Live!

Get 1:1 Help Now