473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Change existing element in XML file

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

May 7 '07 #1
5 13163
On May 7, 10:29 am, yhl...@gmail.com wrote:
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

May 7 '07 #2
yh****@gmail.com wrote:
<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.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 7 '07 #3
On May 7, 5:46 pm, rowe_newsgroups <rowe_em...@yahoo.comwrote:
On May 7, 10:29 am, yhl...@gmail.com wrote:
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
Hi

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

Thanks

May 7 '07 #4
On May 7, 5:59 pm, Martin Honnen <mahotr...@yahoo.dewrote:
yhl...@gmail.com wrote:
<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.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
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

May 7 '07 #5
yh****@gmail.com wrote:
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"
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: KemperR | last post by:
Dear Experts, I'm new to docbook and have sucessfully started to get into customisation layers. Therefore I changed the dtd as recomended. But I got stuck at the simple point when I want to add...
8
by: yinjennytam | last post by:
Hi all, I'm new to .NET and XML and I have a question. Given an XML file, I want to navigate its content and look for one or two particular elements to get their values. At this point, it...
4
by: Grant | last post by:
Gday, I am using the sample code at: http://support.microsoft.com/default.aspx?scid=kb;en-us;317666 to modify an existing XML document. The XML document looks like the following: ...
3
by: Frankie | last post by:
I'm writing a small utility app (C# Windows forms) used to create new ASP.NET Web sites. This utility needs to be able to change existing values in an existing Web.config. Please note: I do NOT...
1
by: onemonk | last post by:
hi guys... I hav this problem, I am planning to write html form values to xml file and... the problem is... When I am entering data... its recording only the latest entry.. by overwriting the...
2
by: Lee | last post by:
I have an xml Structure: <?xml version="1.0" encoding="utf-16"?> <Request> <Operations> <Command> <OpType>login</OpType> <Params> <UserName>leebo</UserName> <Password>leebo</Password>
3
by: mchapman13 | last post by:
I am VERY new to XML and I am very confussed. Hoping someone could shead some light on my problems. IS this even do-able and if so what am i doing wrong WHAT CURRENTLY EXISTS: I have an...
4
by: Anand | last post by:
Hi, I'm new to Python. we are using python2.4. I wanted to insert an element into an existing xml file. Can anyone help me with this? I've seen lxml and elementTree in python2.5 has some...
1
by: ofuuzo1 | last post by:
Hi, Is there anyway I can append a new element to an existing xml without first loading the existing file into a variable, adding the new element into the variable and saving it by overwriting the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.