473,569 Members | 2,400 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>Whatev er</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.Rea dXml(Server.Map Path("/") + @"\Site.xml" );
XmlDataDocument xddSiteVars = new XmlDataDocument (dstSiteVars);
XmlNodeList xnlSiteVars = xddSiteVars.Get ElementsByTagNa me(varName);
if (xnlSiteVars.Co unt == 1) {
return xnlSiteVars.Ite m(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(s tring 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 1154
Alan try looking through this article at :-
http://aspnet.4guysfromrolla.com/articles/112603-1.aspx
It should guide you..
Patrick

"Alan Silver" <al*********@no spam.thanx> wrote in message
news:Zv******** ******@nospamth ankyou.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>Whatev er</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.Rea dXml(Server.Map Path("/") + @"\Site.xml" );
XmlDataDocument xddSiteVars = new XmlDataDocument (dstSiteVars);
XmlNodeList xnlSiteVars = xddSiteVars.Get ElementsByTagNa me(varName);
if (xnlSiteVars.Co unt == 1) {
return xnlSiteVars.Ite m(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(s tring 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*********@no spam.thanx> wrote in message
news:Zv******* *******@nospamt hankyou.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>Whatev er</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.Rea dXml(Server.Map Path("/") + @"\Site.xml" );
XmlDataDocument xddSiteVars = new XmlDataDocument (dstSiteVars);
XmlNodeList xnlSiteVars = xddSiteVars.Get ElementsByTagNa me(varName);
if (xnlSiteVars.Co unt == 1) {
return xnlSiteVars.Ite m(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(s tring 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(s iteVarsFile);
// create a string containing the contents of the file
string siteVars = "";
string nextLine;
while ((nextLine = strSiteVars.Rea dLine()) != null) {
siteVars += nextLine;
}
// create a new XML document that will hold the contents of the file
XmlDocument xdcSiteVars = new XmlDocument();
xdcSiteVars.Loa dXml(siteVars);
// try changing the value of the element whose name was passed in
try {
xdcSiteVars.Doc umentElement[varName].InnerXml = varValue;
} catch (NullReferenceE xception) {
// if we got a NullReferenceEx ception then the node doesn't exist, so create it
XmlElement xelNew = xdcSiteVars.Cre ateElement(varN ame);
xelNew.InnerTex t = varValue;
xdcSiteVars.Doc umentElement.Ap pendChild(xelNe w);
}
// write out the file again
XmlTextWriter xwrWriter = new XmlTextWriter(s iteVarsFile, null);
xwrWriter.Forma tting = Formatting.Inde nted;
xdcSiteVars.Sav e(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(s iteVarsFile);
// create a string containing the contents of the file
string siteVars = "";
string nextLine;
while ((nextLine = strSiteVars.Rea dLine()) != null) {
siteVars += nextLine;
}
// create a new XML document that will hold the contents of the file
XmlDocument xdcSiteVars = new XmlDocument();
xdcSiteVars.Lo adXml(siteVars) ;
// try changing the value of the element whose name was passed in
try {
xdcSiteVars.Doc umentElement[varName].InnerXml = varValue;
} catch (NullReferenceE xception) {
// if we got a NullReferenceEx ception then the node doesn't exist, so
create it
XmlElement xelNew = xdcSiteVars.Cre ateElement(varN ame);
xelNew.InnerTex t = varValue;
xdcSiteVars.Doc umentElement.Ap pendChild(xelNe w);
}
// write out the file again
XmlTextWrite r xwrWriter = new XmlTextWriter(s iteVarsFile, null);
xwrWriter.Form atting = Formatting.Inde nted;
xdcSiteVars.Sa ve(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
6386
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 single article which can be viewed at http://www.tonymarston.net/php-mysql/good-bad-oop.html I fully expect this to be the start of another flame...
1
1135
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 want to stor the information in an SQL Database and have created a very simple table with with the following fields: menuID
1
1233
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 as follows: Dim MyWebClient As New System.Net.WebClient MyWebClient.DownloadFile(StringOfCSVURL, "c:\testdl.csv")
15
3478
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 abstract - it's going right over my head. Can somebody explain, in simple/concrete terms what MS SharePoint is? -- PeteCresswell
7
2107
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 database that you want, and send it as a GET request. This would be an example: ...
669
25670
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 paper written on this subject. On the Expressive Power of Programming Languages, by Matthias Felleisen, 1990....
2
2745
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 <string>
10
13612
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
1579
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 again? FWIW, here's the list of files that were created after running py2exe: myprog.exe bz2.pyd
0
7618
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
7926
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
7679
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
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
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
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
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 we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.