Help with adding data elements in string variable using VB.NET

vcharles
vcharles used Ask the Experts™
on
Hi,

 How do you modify the code below to add data elements from a string variable (i.e. S = Effect, NSC, NSN, AGD)?

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
         Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP5.xml")
         Dim results = (From n In xdoc.Root.Descendants("AOP5")
                        Select n).ToList()
         Dim resultsa = (From n In xdoc.Root.Descendants("Effect")
                     Select n).Count()

         If resultsa = 0 Then
         results.ForEach(Sub(n) n.Add(<Effect></Effect>))
         End If

         xdoc.Save(Application.StartupPath + "\AOP5.xml")

Open in new window


 Thanks,

Victor
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Help.
Software Engineer
Commented:
Well you could do it this way:
        Dim results = (From n In xdoc.Root.Descendants("AOP5")
                       Select n).ToList()

        Dim S As String = "Effect, NSC, NSN, AGD"
        For Each sElem As String In S.Split({","c, " "c}, StringSplitOptions.RemoveEmptyEntries)
            Dim resultsa = (From n In xdoc.Root.Descendants(sElem)
                        Select n).Count()
            If resultsa = 0 Then
                Dim newElem As String = sElem ' copy for use in lamba expression as per warning given by the compiler/IDE
                results.ForEach(Sub(n) n.Add(New XElement(newElem)))
            End If
        Next

Open in new window


However, judging from your other open question, you may be wanting to add each of them if not present in a subnode  instead of what the code does now: add each new element if it is not present anywhere  in the document. If that's the case, try the following:
        Dim results = (From n In xdoc.Root.Descendants("AOP5")
                       Select n).ToList()

        Dim S As String = "Effect, NSC, NSN, AGD"
        For Each sElem As String In S.Split({","c, " "c}, StringSplitOptions.RemoveEmptyEntries)
            Dim newElem As String = sElem ' copy for use in lamba expression as per warning given by the compiler/IDE
            results.ForEach(Sub(n) If n.Descendants(newElem).Count = 0 Then n.Add(New XElement(newElem)))
        Next

Open in new window

Author

Commented:
Hi,

It worked. Thank You.

How do you modify the code to remove all Data elements in File1 not found in a string variable  S = "Effect, NSC, NSN, AGD"?

Victor
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
Thank You!
Robert SchuttSoftware Engineer

Commented:
How do you modify the code to remove all Data elements in File1 ...
Which one did you choose?

Can you give an example input file?

EDIT: and output file?

Author

Commented:
I chose the second one, better incase dataelment already exist.

Thanks,

Victor

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial