Help with removing Data elemnts not included in string variable using VB.NET

Hi,

How do you modify the code to remove all Data elements in an xml file not found in a string variable  S = "Effect, NSC, NSN, AGD" using VB.NET?

Thanks,

 Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Victor;

This should work.
'' Load the XML document into memory
Dim xdoc = XDocument.Load("C:\Working Directory\test.xml")                       
'' Create a list of tag names to remove from the document
Dim tagNamesToDelete As New List(Of String) From {"Effect", "NSC", "NSN", "AGD" }

'' Get a list of XElements to be removed from the document                                                                                 
Dim results = (From n In xdoc.Descendants()                                      
               Where tagNamesToDelete.Contains(n.Name.LocalName)                 
               Select n).ToList()                                                
                                                         
'' Remove the XElements                                                                                 
results.ForEach(Sub(n) n.Remove())                                               
'' Save the XDocument with nodes removed                                                                                 
xdoc.Save("File Name and Path Here")

Open in new window

0
vcharlesAuthor Commented:
Hi Fernando,

Thanks for the code, unfortunately it's working the opposite way, I would like to delete all data elements not included in the string ({"Effect", "NSC", "NSN", "AGD" }). Instead the code deletes the data elements in the string.

Victor
0
Fernando SotoRetiredCommented:
Then change the query like this.
Dim results = (From n In xdoc.Descendants("Table1").Elements()
               Where Not tagNamesToDelete.Contains(n.Name.LocalName)
               Select n).ToList()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

vcharlesAuthor Commented:
Thanks, I will try it when i get home. Also how do you achieve the same if the data elements are in an xml file?

I can open if new case if you prefer.

Thanks.
0
Fernando SotoRetiredCommented:
That was in an XML file.
0
vcharlesAuthor Commented:
Hi,

I meant, using values from file2.xml

<Table>
<Effect>
<NSC>
<NSN>
<AGD>
<Table>

I will try the codr and get back to you.

Victor
0
vcharlesAuthor Commented:
Thank You.
0
Fernando SotoRetiredCommented:
Hi Victor, can you please open a new question for your last post. Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.