Solved

Help with changing table name in xml file

Posted on 2013-10-25
6
214 Views
Last Modified: 2013-10-26
Hello,

I have an xml file with <Row> as the table name, How do I change it to <Country> using VB.NET?

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
  • 3
  • 3
6 Comments
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39600436
Hi Victor;

A little bit more information is needed to answer this question.

Can you post the structure of the XML document?
Is the XML document in a String variable in your code or in a file on your system?
0
 

Author Comment

by:vcharles
ID: 39600453
Hi,

Thye xml file is in the following format:

<Root>
<Row>
<Table_ID>1</Table_ID>
<Item1></Item1>
<Item2></Item2>
</Row>
<Row>
<Table_ID>2</Table_ID>
<Item1></Item1>
<Item2></Item2>
</Row>
<Row>
<Table_ID>3</Table_ID>
<Item1></Item1>
<Item2></Item2>
</Row>
</Root>
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39600678
Hi vcharles;

Here is a way to do what you want.

Imports System.Xml.Linq 

' XML as a string in code
Dim xmlStr = "<Root><Row><Table_ID>1</Table_ID><Item1></Item1><Item2></Item2></Row><Row><Table_ID>2</Table_ID><Item1></Item1><Item2></Item2></Row><Row><Table_ID>3</Table_ID><Item1></Item1><Item2></Item2></Row></Root>"
' Convert string to XML document
Dim xdoc = XElement.Parse(xmlStr)
' Get list of nodes to be replaced 
Dim rows = xdoc.Descendants("Row").ToList()
' Create the new Country node with the children of the old Row node
' and replace in original XML document
For i As Integer = 0 To rows.Count() - 1
    Dim newElement = New XElement("Country", rows(i).Elements())
    rows(i).ReplaceWith(newElement)
Next

' Convert the modified XML back to a string
Dim newXMLStr = xdoc.ToString()

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:vcharles
ID: 39600684
Thank You, will try it and get back to you.
0
 

Author Closing Comment

by:vcharles
ID: 39602824
Thank You.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39602959
Not a problem Victor, glad to help.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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