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

A

Alan Silver

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
 
A

Alan Silver

Alan try looking through this article at :-

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.
 
A

Alan Silver

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);
 
A

Alan Silver

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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top