Solved

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

Posted on 2015-01-24
7
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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