Solved

Help with appending values in data element of xml file using VB.NET

Posted on 2015-01-27
4
109 Views
Last Modified: 2016-02-17
Hi,

How do you add 130500 in front of NSC data element of an xml file? For example if I have the following xml file.

<Root>
<AOP5>
<ID></ID>
<SN></SN>
<NSC>8970</NSC>
</AOP5>
<AOP5>
<ID></ID>
<SN></SN>
<NSC>8789</NSC>
</AOP5>
<AOP5>
<ID></ID>
<SN></SN>
<NSC>7865</NSC>
</AOP5>
</Root>

How do I obtain:

<Root>
<AOP5>
<ID></ID>
<SN></SN>
<NSC>1305008970</NSC>
</AOP5>
<AOP5>
<ID></ID>
<SN></SN>
<NSC>1305008789</NSC>
</AOP5>
<AOP5>
<ID></ID>
<SN></SN>
<NSC>1305007865</NSC>
</AOP5>
</Root>

Thanks,

Victor
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
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 40573075
Old school way would be:
        Dim dom As New XmlDocument
        dom.Load("C:\temp\test.xml")

        Dim nodes As XmlNodeList = dom.SelectNodes("//NSC")
        For Each node As XmlNode In nodes
            node.InnerText = "130500" & node.InnerText
        Next

        dom.Save("C:\temp\test.xml")

Open in new window

I can knock together a Linq-to-Xml version if you prefer.
0
 

Author Comment

by:vcharles
ID: 40573111
Hi,

Can you please also send me the Linq-to-Xml version.

Thanks,

V.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40573198
It's not that much different in this scenario, but here it is:
        Dim dom As XDocument = XDocument.Load("C:\temp\test.xml")

        Dim nodes As IEnumerable(Of XElement) = dom.Descendants("NSC")
        For Each node As XElement In nodes
            node.SetValue("130500" + node.Value)
        Next

        dom.Save("C:\temp\test.xml")

Open in new window

It would be slicker in C# because there is a foreach extension method, but hey ho!
0
 

Author Closing Comment

by:vcharles
ID: 40573376
Thank You.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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