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


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?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

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.

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

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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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.

Fernando SotoRetiredCommented:
That was in an XML file.
vcharlesAuthor Commented:

I meant, using values from file2.xml


I will try the codr and get back to you.

vcharlesAuthor Commented:
Thank You.
Fernando SotoRetiredCommented:
Hi Victor, can you please open a new question for your last post. Thanks
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.