Solved

Help with changing table name in xml file

Posted on 2013-10-25
6
210 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

15 Experts available now in Live!

Get 1:1 Help Now