473,563 Members | 2,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13180
On May 7, 10:29 am, yhl...@gmail.co m 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 GetElementByTag Name method.

Thanks,

Seth Rowe

May 7 '07 #2
yh****@gmail.co m 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("fi le.xml")
XmlDoc.Document Element("Path") .InnerText = "test2"
XmlDoc.Save("fi le.xml")

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

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 7 '07 #3
On May 7, 5:46 pm, rowe_newsgroups <rowe_em...@yah oo.comwrote:
On May 7, 10:29 am, yhl...@gmail.co m 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 GetElementByTag Name 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...@yaho o.dewrote:
yhl...@gmail.co m 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("fi le.xml")
XmlDoc.Document Element("Path") .InnerText = "test2"
XmlDoc.Save("fi le.xml")

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

--

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.Document Element("elem2" ).InnerText = "test2"
or
XmlDoc.Document Element("elem1/elem2").InnerTe xt = "test2"

Thanks

May 7 '07 #5
yh****@gmail.co m 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.Document Element("elem2" ).InnerText = "test2"
You can only find child nodes with the index syntax so you would need
XmlDoc.Document Element("elem1" )("elem2").Inne rText = "test2"
or, if you want to use XPath expressions then you need to use
SelectSingleNod e
XmlDoc.Document Element("elem1/elem2").InnerTe xt = "test2"
XmlDoc.Document Element.SelectS ingleNode("elem 1/elem2").InnerTe xt =
"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
1053
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 existing classes to my new elements. E.g I would like to see every thing of the para.class to my new element eeabastract. Please find my DTD below:...
8
5778
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 suffices to open the XML file for read-only access. Once I have processed these values, I might need to update a bunch of subelements of a certain...
4
1789
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: --------------------------- <parameters> <Config-File> <Value>c:\config.xml</Value>
3
13389
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 want to make this change from any currently running Web application. Rather, I plan to have a "base/standard Web.config" that is copied then updated by...
1
1408
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 existing entry.. so at any given time there's only one element in the xml file... I repeat my self... Everytime a new entry is added, the already...
2
1275
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
2242
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 existing XSD file that has all my XML tags that currently exist in my DB, it has the correct structrure for recursive tags (ex: if a person can have more...
4
2579
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 API's to insert into existing xml file. We cant use python 2.5 due to some limitations. So how can we do it in 2.4?
1
3779
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 existing file name? Thanks Ofuuzo
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7634
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6244
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.