473,473 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

easietway of writing to an xml file

Hi
what is the easiest way of changing the value of an element in an xml file
using C#?

thanks for your help.
ALI
Nov 12 '05 #1
2 1366
Hi ALI

DOM will probably do what you want - unless your documents are large...code
follows

HTH

Nigel

Document test.xml
<?xml version='1.0' encoding='utf-8' ?>
<root><test>Old</test></root>

//DOM code
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
System.Xml.XmlElement el =
(System.Xml.XmlElement)d.GetElementsByTagName("tes t")[0];
el.InnerText = "New";
d.Save(@"C:\test.xml");

"ALI-R" wrote:
Hi
what is the easiest way of changing the value of an element in an xml file
using C#?

thanks for your help.
ALI

Nov 12 '05 #2
Hi,,
This is exactly what I am doing now ,,but the problwm is that before doing
this I am reading the xml document by using the following code
====================Start of Code=====================

XmlTextReader reader = new XmlTextReader("AppSetting.xml");

while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "ConnectionString":
{
applicationVars.ConnectionString = reader.ReadElementString().Trim();
break;
}

}
}
reader.Close();
====================End of Code=====================

then I try to update an element using your code but I get this Error:
the procescan not access " " because it is being used by anotheprocess

Thanks for your help.
ALI
"Nigel Armstrong" <Ni************@discussions.microsoft.com> wrote in
message news:E9**********************************@microsof t.com...
Hi ALI

DOM will probably do what you want - unless your documents are large...code follows

HTH

Nigel

Document test.xml
<?xml version='1.0' encoding='utf-8' ?>
<root><test>Old</test></root>

//DOM code
System.Xml.XmlDocument d = new System.Xml.XmlDocument();
d.Load(@"C:\test.xml");
System.Xml.XmlElement el =
(System.Xml.XmlElement)d.GetElementsByTagName("tes t")[0]; el.InnerText = "New";
d.Save(@"C:\test.xml");

"ALI-R" wrote:
Hi
what is the easiest way of changing the value of an element in an xml file using C#?

thanks for your help.
ALI

Nov 12 '05 #3

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
12
by: glennanthonyb | last post by:
Hi The company I work for has finally woken up to data security on our field laptops. I'm writing something in C# that will allow remote deletion of sensitive data and I don't believe...
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
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...
1
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...
1
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...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.