Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with removing multiple data elements  from xm file

Posted on 2015-01-27
4
Medium Priority
?
98 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 64

Accepted Solution

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

Expert Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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