Change existing element in XML file

  • Thread starter Thread starter yhlove
  • Start date Start date
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
 
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
 
<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.
 
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
 
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
 
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"
 
Back
Top