Help with changing table name using vb.net

Hi,

How do you change a yable name in an xml file and save the chang3 in a new file using vb.net?

Thanks,
Victor
vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Can you please post some more information about your concrete context?

You can use XDocument to load your file and change the appropriate elements content.

E.g.

Imports System.Xml.XPath
Module Module1

    Sub Main()

        'Dim xmlDocument As XDocument = XDocument.Load() using an appropriate file reader.
        Dim xmlDocument As XDocument =
            <?xml version="1.0" encoding="UTF-8"?>
            <root>
                <table>
                    <name>test</name>
                </table>
            </root>

        xmlDocument.XPathSelectElement("/root/table/name").Value = "TEST"
        Console.WriteLine(xmlDocument.ToString())

        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

End Module

Open in new window

vcharlesAuthor Commented:
Hi,
I am able to run a DLL file from another another windoes  App by using it as a reference. I would like run the sane file using a web link, trying to find out the best approach.
Fernando SotoRetiredCommented:
Hi Victor;

Using one of your old example XML file this is an example of how to change an XML node Name/Tag to another name.

'' Load the XML document into memory from the file system
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\Victor.xml")

'' Query the XML for all the table names 
Dim tablesNames As List(Of XElement) = (From t In xdoc.Descendants("Table1")
                                        Select T).ToList()

'' xdoc has nodes of Table1 we wish to rename to NewTable2
For Each tableNode As XElement In tablesNames
    '' Update the node name from Table1 to NewTable2
    tableNode.Name = "NewTable2"
Next

tablesNames.Dump()
'' Save the updated document back to the file system under a new name
xdoc.Save("C:\Working Directory\VictorWithNewTablenames.xml")

Open in new window

XML file to change table names
<Root>
  <Table1>
    <ID>1</ID>
    <SN>10411</SN>
    <ItemA>DATA</ItemA>
    <ItemX>DATAX</ItemX>
    <ItemY>DATAY</ItemY>
    <ItemZ>DATAZ</ItemZ>
    <SN>10411</SN>
    <ItemXX>A</ItemXX>
    <ItemYY>B</ItemYY>
    <ItemZZ>C</ItemZZ>
  </Table1>
  <Table1>
    <ID>2</ID>
    <SN>10411</SN>
    <ItemA>DATA1</ItemA>
    <ItemX>DATAX1</ItemX>
    <ItemY>DATAY1</ItemY>
    <ItemZ>DATAZ1</ItemZ>
    <SN>10411</SN>
    <ItemXX>D</ItemXX>
    <ItemYY>E</ItemYY>
    <ItemZZ>F</ItemZZ>
  </Table1>
  <Table1>
    <ID>3</ID>
    <SN>10412</SN>
    <ItemA>DATA2</ItemA>
    <ItemX>DATAX2</ItemX>
    <ItemY>DATAY2</ItemY>
    <ItemZ>DATAZ2</ItemZ>
    <SN>10412</SN>
    <ItemXX>G</ItemXX>
    <ItemYY>H</ItemYY>
    <ItemZZ>I</ItemZZ>
  </Table1>
</Root>

Open in new window

Resulting XML file
<Root>
  <NewTable2>
    <ID>1</ID>
    <SN>10411</SN>
    <ItemA>DATA</ItemA>
    <ItemX>DATAX</ItemX>
    <ItemY>DATAY</ItemY>
    <ItemZ>DATAZ</ItemZ>
    <SN>10411</SN>
    <ItemXX>A</ItemXX>
    <ItemYY>B</ItemYY>
    <ItemZZ>C</ItemZZ>
  </NewTable2>
  <NewTable2>
    <ID>2</ID>
    <SN>10411</SN>
    <ItemA>DATA1</ItemA>
    <ItemX>DATAX1</ItemX>
    <ItemY>DATAY1</ItemY>
    <ItemZ>DATAZ1</ItemZ>
    <SN>10411</SN>
    <ItemXX>D</ItemXX>
    <ItemYY>E</ItemYY>
    <ItemZZ>F</ItemZZ>
  </NewTable2>
  <NewTable2>
    <ID>3</ID>
    <SN>10412</SN>
    <ItemA>DATA2</ItemA>
    <ItemX>DATAX2</ItemX>
    <ItemY>DATAY2</ItemY>
    <ItemZ>DATAZ2</ItemZ>
    <SN>10412</SN>
    <ItemXX>G</ItemXX>
    <ItemYY>H</ItemYY>
    <ItemZZ>I</ItemZZ>
  </NewTable2>
</Root>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Hi Victor;

Your last post on this thread is for the wrong question.
vcharlesAuthor Commented:
Hi Fernando,  

Thank you for the solution, will repost wrong question

Victor
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.