Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2015-01-27
4
Medium Priority
?
113 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

916 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