Solved

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

Posted on 2015-01-27
4
104 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
  • 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

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 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…
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…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

816 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