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

Discussion in 'ASP .Net' started by Alan Silver, Aug 9, 2005.

  1. Alan Silver

    Alan Silver Guest

    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, Aug 9, 2005
    #1
    1. Advertising

  2. Alan try looking through this article at :-
    http://aspnet.4guysfromrolla.com/articles/112603-1.aspx
    It should guide you..
    Patrick

    "Alan Silver" <> wrote in message
    news:...
    > 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)
     
    Patrick.O.Ige, Aug 10, 2005
    #2
    1. Advertising

  3. Alan Silver

    Alan Silver Guest

    >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" <> wrote in message
    >news:...
    >> 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)
     
    Alan Silver, Aug 10, 2005
    #3
  4. Alan Silver

    Alan Silver Guest

    >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)
     
    Alan Silver, Aug 10, 2005
    #4
  5. Alan Silver

    Alan Silver Guest

    >>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)
     
    Alan Silver, Aug 10, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brent Burkart
    Replies:
    5
    Views:
    792
    Jerry III
    Oct 16, 2003
  2. =?Utf-8?B?VGltOjouLg==?=

    Simplest Solution for Repeat Update???

    =?Utf-8?B?VGltOjouLg==?=, Sep 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    340
    S. Justin Gengo
    Sep 28, 2005
  3. Andrea Francia
    Replies:
    0
    Views:
    334
    Andrea Francia
    Feb 8, 2008
  4. Andrea Francia
    Replies:
    12
    Views:
    586
  5. Replies:
    3
    Views:
    139
    Joel VanderWerf
    Nov 16, 2006
Loading...

Share This Page