Solved

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

Posted on 2015-01-24
7
303 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 62

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 62

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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 62

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now