Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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