?
Solved

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

Posted on 2015-01-24
7
Medium Priority
?
346 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 2000 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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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