Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > What's the simplest way to update an XML file?

Reply
Thread Tools

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

 
 
Alan Silver
Guest
Posts: n/a
 
      08-09-2005
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)
 
Reply With Quote
 
 
 
 
Patrick.O.Ige
Guest
Posts: n/a
 
      08-10-2005
Alan try looking through this article at :-
http://aspnet.4guysfromrolla.com/articles/112603-1.aspx
It should guide you..
Patrick

"Alan Silver" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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)



 
Reply With Quote
 
 
 
 
Alan Silver
Guest
Posts: n/a
 
      08-10-2005
>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" <(E-Mail Removed)> wrote in message
>news:(E-Mail Removed)...
>> 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)
 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      08-10-2005
>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)
 
Reply With Quote
 
Alan Silver
Guest
Posts: n/a
 
      08-10-2005
>>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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ruby newbie & XML - What's the simplest way to get XML data into a hash? darenbell@gmail.com Ruby 3 11-16-2006 11:23 PM
Simplest Solution for Repeat Update??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 1 09-28-2005 10:53 AM
+Simplest+ way to attributes to all of a particular Web Form control type? ASP .Net 2 09-16-2005 03:38 AM
What's the simplest way to do URL rewriting? Alan Silver ASP .Net 9 06-26-2005 02:16 PM
simplest way to password protect website with SQL Server Brent Burkart ASP .Net 5 10-16-2003 05:34 AM



Advertisments