Solved

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

Posted on 2015-01-27
4
103 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank You.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

728 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

10 Experts available now in Live!

Get 1:1 Help Now