473,507 Members | 11,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the simplest way to update an XML file?

Hello,

I have a site that stores some info in an XML file. The file is pretty
simple, of the form...

<Site>
<SiteName>Fred's Ferrets</SiteName>
<SomeVar>Whatever</SomeVar>
.... etc ...
</Site>

So far I have just used a simple routine to read values from the file...

public string SiteVars(string varName) {
DataSet dstSiteVars = new DataSet();
dstSiteVars.ReadXml(Server.MapPath("/") + @"\Site.xml");
XmlDataDocument xddSiteVars = new XmlDataDocument(dstSiteVars);
XmlNodeList xnlSiteVars = xddSiteVars.GetElementsByTagName(varName);
if (xnlSiteVars.Count == 1) {
return xnlSiteVars.Item(0).InnerText;
} else {
return "";
}
}

This is fine, but now I want to be able to update/add values. I have
looked around a bit, but all the code is so complex that I can't help
but feel that it's OTT for what I need.

Basically I would like to write a companion method for the above...

public void UpdateSiteVar(string varName, string newValue) {
....
}

That when called would either update the existing value if it exists, or
add the new node/value to the file if it doesn't already exist.

Can anyone help me here please? I'm sure it's really simple, but I'm
getting swamped with large examples.

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
4 1150
Alan try looking through this article at :-
http://aspnet.4guysfromrolla.com/articles/112603-1.aspx
It should guide you..
Patrick

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:Zv**************@nospamthankyou.spam...
Hello,

I have a site that stores some info in an XML file. The file is pretty
simple, of the form...

<Site>
<SiteName>Fred's Ferrets</SiteName>
<SomeVar>Whatever</SomeVar>
... etc ...
</Site>

So far I have just used a simple routine to read values from the file...

public string SiteVars(string varName) {
DataSet dstSiteVars = new DataSet();
dstSiteVars.ReadXml(Server.MapPath("/") + @"\Site.xml");
XmlDataDocument xddSiteVars = new XmlDataDocument(dstSiteVars);
XmlNodeList xnlSiteVars = xddSiteVars.GetElementsByTagName(varName);
if (xnlSiteVars.Count == 1) {
return xnlSiteVars.Item(0).InnerText;
} else {
return "";
}
}

This is fine, but now I want to be able to update/add values. I have
looked around a bit, but all the code is so complex that I can't help
but feel that it's OTT for what I need.

Basically I would like to write a companion method for the above...

public void UpdateSiteVar(string varName, string newValue) {
...
}

That when called would either update the existing value if it exists, or
add the new node/value to the file if it doesn't already exist.

Can anyone help me here please? I'm sure it's really simple, but I'm
getting swamped with large examples.

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
>Alan try looking through this article at :-
http://aspnet.4guysfromrolla.com/articles/112603-1.aspx
It should guide you..
Thanks Patrick, that's quite clever. He just pulls the XML into a
dataset and uses the native method for writing. I'll have a go at that.

I don't see there's any advantage to using this method for reading the
file though, my code is a lot simpler.

I'll have a play and see if I can get it to work. Thanks again.
Patrick

"Alan Silver" <al*********@nospam.thanx> wrote in message
news:Zv**************@nospamthankyou.spam...
Hello,

I have a site that stores some info in an XML file. The file is pretty
simple, of the form...

<Site>
<SiteName>Fred's Ferrets</SiteName>
<SomeVar>Whatever</SomeVar>
... etc ...
</Site>

So far I have just used a simple routine to read values from the file...

public string SiteVars(string varName) {
DataSet dstSiteVars = new DataSet();
dstSiteVars.ReadXml(Server.MapPath("/") + @"\Site.xml");
XmlDataDocument xddSiteVars = new XmlDataDocument(dstSiteVars);
XmlNodeList xnlSiteVars = xddSiteVars.GetElementsByTagName(varName);
if (xnlSiteVars.Count == 1) {
return xnlSiteVars.Item(0).InnerText;
} else {
return "";
}
}

This is fine, but now I want to be able to update/add values. I have
looked around a bit, but all the code is so complex that I can't help
but feel that it's OTT for what I need.

Basically I would like to write a companion method for the above...

public void UpdateSiteVar(string varName, string newValue) {
...
}

That when called would either update the existing value if it exists, or
add the new node/value to the file if it doesn't already exist.

Can anyone help me here please? I'm sure it's really simple, but I'm
getting swamped with large examples.

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)



--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #3
>I'll have a play and see if I can get it to work. Thanks again.

In case anyone's looking for something like this, here is my humble
attempt. It works fine, but if anyone more experienced than me (ie
everyone here!!) has any comments, or suggestions for improvement, I
would be happy to hear them. I'm not sure this is the simplest way to do
this, but it was the simplest I could find.

The way this works is that it is part of a method that takes two string
variables, varName and varValue, which are the name and value of the
node of interest. The code tries changing the value, catching the
exception that occurs if the node doesn't already exist.

// get a reader for the file
StreamReader strSiteVars = File.OpenText(siteVarsFile);
// create a string containing the contents of the file
string siteVars = "";
string nextLine;
while ((nextLine = strSiteVars.ReadLine()) != null) {
siteVars += nextLine;
}
// create a new XML document that will hold the contents of the file
XmlDocument xdcSiteVars = new XmlDocument();
xdcSiteVars.LoadXml(siteVars);
// try changing the value of the element whose name was passed in
try {
xdcSiteVars.DocumentElement[varName].InnerXml = varValue;
} catch (NullReferenceException) {
// if we got a NullReferenceException then the node doesn't exist, so create it
XmlElement xelNew = xdcSiteVars.CreateElement(varName);
xelNew.InnerText = varValue;
xdcSiteVars.DocumentElement.AppendChild(xelNew);
}
// write out the file again
XmlTextWriter xwrWriter = new XmlTextWriter(siteVarsFile, null);
xwrWriter.Formatting = Formatting.Indented;
xdcSiteVars.Save(xwrWriter);
--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #4
>>I'll have a play and see if I can get it to work. Thanks again.

In case anyone's looking for something like this, here is my humble
attempt.
As a postscript, I discovered that you need to close the StreamReader
before you can write out the file, otherwise you get an error as another
process (ie the SR) is holding the file when the XmlTextWriter tries to
write it out.

Similarly, it is worth closing the XmlTextWriter as soon as it has
written as this avoids a similar exception if you call the same code
shortly afterwards.

All of which shows the value of closing/disposing of items when you're
done with them I guess!!
It works fine, but if anyone more experienced than me (ie everyone
here!!) has any comments, or suggestions for improvement, I would be
happy to hear them. I'm not sure this is the simplest way to do this,
but it was the simplest I could find.

The way this works is that it is part of a method that takes two string
variables, varName and varValue, which are the name and value of the
node of interest. The code tries changing the value, catching the
exception that occurs if the node doesn't already exist.

// get a reader for the file
StreamReader strSiteVars = File.OpenText(siteVarsFile);
// create a string containing the contents of the file
string siteVars = "";
string nextLine;
while ((nextLine = strSiteVars.ReadLine()) != null) {
siteVars += nextLine;
}
// create a new XML document that will hold the contents of the file
XmlDocument xdcSiteVars = new XmlDocument();
xdcSiteVars.LoadXml(siteVars);
// try changing the value of the element whose name was passed in
try {
xdcSiteVars.DocumentElement[varName].InnerXml = varValue;
} catch (NullReferenceException) {
// if we got a NullReferenceException then the node doesn't exist, so
create it
XmlElement xelNew = xdcSiteVars.CreateElement(varName);
xelNew.InnerText = varValue;
xdcSiteVars.DocumentElement.AppendChild(xelNew);
}
// write out the file again
XmlTextWriter xwrWriter = new XmlTextWriter(siteVarsFile, null);
xwrWriter.Formatting = Formatting.Indented;
xdcSiteVars.Save(xwrWriter);


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #5

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

Similar topics

52
6354
by: Tony Marston | last post by:
Several months ago I started a thread with the title "What is/is not considered to be good OO programming" which started a long and interesting discussion. I have condensed the arguments into a...
1
1127
by: Tim::.. | last post by:
Hi... Can someone please tell me the simplest solution to create an update page for a menu I have on an asp.net website... Basiclly I have 10 menu and 10 price fields that change every day! I...
1
1229
by: Scott | last post by:
Say there is a file on a website as a CSV. Not protected, just a data dump. I'd like to collect this data to my computer to be analyzed. Right now I am using the the MyWebClient feature. Code...
15
3474
by: (PeteCresswell) | last post by:
Got a sample of MS's "Advisor Guide To Microsoft Access" in the mail today - accompanied by a sample "Advisor Guide To Microsoft SharePoint". I skimmed both, but the SharePoint explanation is too...
7
2101
by: lawrence k | last post by:
Okay, I just backed up my database, just in case. The whole schema for the database is here: http://www.accumulist.com/index.php?whatPage=db.php You can run any SELECT query against this...
669
25395
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Language”, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
2
2734
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
10
13573
by: silverburgh.meryl | last post by:
Hi, Can you please tell me what is the mean of 't' in the input parametre in utf8_fopen? utf8_fopen( a_file, "wt" ) Thank you.
2
1576
by: Gilles Ganault | last post by:
Hello Out of curiosity, if I recompile a Python (wxPython) app with py2exe, can I have customers just download the latest .exe, or are there dependencies that require downloading the whole thing...
0
7223
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
7110
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...
0
7314
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
7372
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
7482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5041
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
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
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.