Change existing element in XML file

Y

yhlove

Hi

I'm trying to change element in xml file as described below:

the xml file:
----------------

<MyProg>
<Path>test1</Path>
</MyProg>

I want to change the element Path to test2 instead of test1
How can I do that ? Is there a function which gets full xml path and
new value and change the element in this path?

Thanks
yhlove
 
R

rowe_newsgroups

Hi

I'm trying to change element in xml file as described below:

the xml file:
----------------

<MyProg>
<Path>test1</Path>
</MyProg>

I want to change the element Path to test2 instead of test1
How can I do that ? Is there a function which gets full xml path and
new value and change the element in this path?

Thanks
yhlove

I believe you need to store the element into an XmlNode object and
then change it's innertext property. You can "grab" the node by using
an XmlDocument's GetElementByTagName method.

Thanks,

Seth Rowe
 
M

Martin Honnen

<MyProg>
<Path>test1</Path>
</MyProg>

I want to change the element Path to test2 instead of test1
How can I do that ? Is there a function which gets full xml path and
new value and change the element in this path?

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("file.xml")
XmlDoc.DocumentElement("Path").InnerText = "test2"
XmlDoc.Save("file.xml")

In general to find nodes you can also use XPath and the methods
SelectNodes and SelectSingleNode.
 
Y

yhlove

I believe you need to store the element into an XmlNode object and
then change it's innertext property. You can "grab" the node by using
an XmlDocument's GetElementByTagName method.

Thanks,

Seth Rowe

Hi

Could u please write me example code.
How can store XmlNode to the xml file ? which function to that?

Thanks
 
Y

yhlove

Dim XmlDoc As XmlDocument = New XmlDocument()
XmlDoc.Load("file.xml")
XmlDoc.DocumentElement("Path").InnerText = "test2"
XmlDoc.Save("file.xml")

In general to find nodes you can also use XPath and the methods
SelectNodes and SelectSingleNode.

Hi

It's work! Thanks a lot.

Another problem:
if the xml is look like that:
<MyFile>
<elem1>
<elem2>test1</elem2>
</elem1>
</MyFile>

and I want to change the element elem2 which is son of elem1. How can
I do that?
I tried to do it in that way but it didn't work:
XmlDoc.DocumentElement("elem2").InnerText = "test2"
or
XmlDoc.DocumentElement("elem1/elem2").InnerText = "test2"

Thanks
 
M

Martin Honnen

if the xml is look like that:
<MyFile>
<elem1>
<elem2>test1</elem2>
</elem1>
</MyFile>

and I want to change the element elem2 which is son of elem1. How can
I do that?
I tried to do it in that way but it didn't work:
XmlDoc.DocumentElement("elem2").InnerText = "test2"

You can only find child nodes with the index syntax so you would need
XmlDoc.DocumentElement("elem1")("elem2").InnerText = "test2"
or, if you want to use XPath expressions then you need to use
SelectSingleNode
XmlDoc.DocumentElement("elem1/elem2").InnerText = "test2"

XmlDoc.DocumentElement.SelectSingleNode("elem1/elem2").InnerText =
"test2"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top