Solved

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

Posted on 2015-01-24
7
296 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

9 Experts available now in Live!

Get 1:1 Help Now