Solved

Help with removing multiple data elements  from xm file

Posted on 2015-01-27
4
80 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
ID: 40574398
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
ID: 40574403
Hi Fernando,

I will try it and get back to you.

Thanks,

V.
0
 

Author Closing Comment

by:vcharles
ID: 40574412
Thank You.
0
 
LVL 62

Expert Comment

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

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…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

911 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