Solved

Help with changing table name in xml file

Posted on 2013-10-25
6
211 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
  • 3
  • 3
6 Comments
 
LVL 62

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 62

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
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.

 

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 62

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

772 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