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

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
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.

vcharlesAuthor Commented:
Help.
0
Robert SchuttSoftware EngineerCommented:
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

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
vcharlesAuthor 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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

vcharlesAuthor Commented:
Thank You!
0
Robert SchuttSoftware EngineerCommented:
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?
0
vcharlesAuthor Commented:
I chose the second one, better incase dataelment already exist.

Thanks,

Victor
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.