Solved

Help with removing multiple data elements  from xm file

Posted on 2015-01-27
4
91 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
[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
 
LVL 63

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 63

Expert Comment

by:Fernando Soto
ID: 40575391
Not a problem Victor, glad to help.
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

623 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