Solved

Help with adding multiple data elements in xml file using vb.net

Posted on 2015-01-24
7
315 Views
Last Modified: 2015-01-27
Hi,

How do you modify the code below to add multiple data elements listed in a string variable (i.e. S =  CA, AGD, NID, MHN) ?
Instead of changing results.ForEach(Sub(n) n.Add(<NID></NID>)) for each data element.
 Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP5.xml")
           Dim results = (From n In xdoc.Root.Descendants("AOP5")
                       Select n).ToList()
            results.ForEach(Sub(n) n.Add(<NID></NID>))
        xdoc.Save(Application.StartupPath + "\AOP5.xml")

Open in new window

0
Comment
Question by:vcharles
  • 4
  • 3
7 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40568151
Do you mean that you want to add multiple nodes like this

<CA></CA>
<AGD></AGD>
<NID></NID>
<MHN></MHN>

Or something else?
0
 

Author Comment

by:vcharles
ID: 40568159
Hi Fernando,
Happy Belated  New Year!
Yes, need to add as shown on your post.
Victor
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40568202
Hi Victor;

And a Happy belated New Years to you and your family.

The following code snippet should do what you need.

'' List of new nodes to be added to a document
Dim S As String = "CA, AGD, NID, MHN"
'' Convert the string to a List(Of String)
Dim SList As New List(Of String)()
SList = S.Split(",").ToList()

'' Load the Document 
Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP5.xml")
'' Get a reference to the AOP5 nodes for new nodes to be added to
Dim results = (From n In xdoc.Root.Descendants("AOP5")
               Select n).ToList()

'' Iterate through the AOP5 nodes
For Each node As XElement In results
    '' Add the new nodes whos names are in SList
    For Each newNode As String In SList
        node.Add(New XElement(newNode.Trim()))
    Next
Next

'' Asve the resulting document
xdoc.Save(Application.StartupPath + "\AOP5.xml")

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:vcharles
ID: 40568277
Thank you. I will try the same approach to remove multiple data elements.
0
 

Author Closing Comment

by:vcharles
ID: 40568279
Thank You.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40568322
Not a problem Victor, always glad to help.
0
 

Author Comment

by:vcharles
ID: 40573545
Hi Fernando,

How do I modify the code below to add data element only if does not already exist?

'' List of new nodes to be added to a document
        Dim S As String = "F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20"
        '' Convert the string to a List(Of String)
        Dim SList As New List(Of String)()
        SList = S.Split(",").ToList()

        '' Load the Document 
        Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AOP40XYZB.xml")
        '' Get a reference to the AOP5 nodes for new nodes to be added to
        Dim results = (From n In xdoc.Root.Descendants("NewDataSet")
                       Select n).ToList()

        '' Iterate through the AOP5 nodes
        For Each node As XElement In results
            '' Add the new nodes whos names are in SList
            For Each newNode As String In SList
                node.Add(New XElement(newNode.Trim()))
            Next
        Next

        '' Asve the resulting document
        xdoc.Save(Application.StartupPath + "\AOP40XYZGG.xml")

Open in new window


Thanks,

Victor
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question