Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

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

Avatar of Victor  Charles

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
That was in an XML file.
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
Thank You.
Hi Victor, can you please open a new question for your last post. Thanks