Solved

Help with removing multiple data elements  from xm file

Posted on 2015-01-27
4
85 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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