How to read an XML file to check for updates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • HaLo2FrEeEk
    Contributor
    • Feb 2007
    • 404

    #1

    How to read an XML file to check for updates

    Hey, I'm writing a program and I'm trying to build in the ability to update it. I've got a menu item called Update and when you click it it should download an xml file from my site and read it. This is what the xml fil would look like:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
      <updateInfo>
        <currentMajorVersion>1</currentMajorVersion>
        <currentMinorVersion>0</currentMinorVersion>
        <currentRevision>0</currentRevision>
        <download>http://example.com/download.zip</download>
      </updateInfo>
    When the person clicks the update button, the program would check it's own internal major, minor, and revision numbers against those read from the xml file. The problem is, I can't figure out how to read the darned xml file!

    I want to navigate into the updateInfo node and read it's child nodes, but I can't get it to work. I have no idea what I'm doing wrong with this. Can someone please help me out!
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Have a look through this thread...



    Both insertAlias and myself recently posted code examples on how to parse an XML file. His method uses LINQ (great for searching through XML) and my method uses System.Xml (more suited to a full traversal imo, but could be argued either way).

    Have a look and let us know if you have any questions :)

    Comment

    • HaLo2FrEeEk
      Contributor
      • Feb 2007
      • 404

      #3
      Ergh, I hate when this happens. I usually continue to tinker around with things even after I post for help and I usually end up figuring something out. Maybe the way I did it was wrong, please let me know if it is, but I did this:

      Code:
      XmlNodeList updateInfo = updateXml.DocumentElement.ChildNodes;
      int updateMajor = Convert.ToInt32(updateInfo[0].InnerText);
      int updateMinor = Convert.ToInt32(updateInfo[1].InnerText);
      int updateRevision = Convert.ToInt32(updateInfo[2].InnerText);
      string updateDownload = updateInfo[3].InnerText;
      if (updateMajor > majorVersion || updateMinor > minorVersion || updateRevision > revision)
      {
          DialogResult update = MessageBox.Show("Your version: " + majorVersion + "." + minorVersion + "." + revision + Environment.NewLine +
              "Update Version: " + updateMajor + "." + updateMinor + "." + revision + Environment.NewLine + Environment.NewLine +
              "Would you like to download the update?", "An update is available!", MessageBoxButtons.YesNo);
          if (update == DialogResult.Yes)
          {
              System.Diagnostics.Process.Start(updateDownload);
          }
      }
      else
      {
          MessageBox.Show("You are using the most current version");
      }
      updateResponse.Close();
      responseStream.Close();
      And I could modify it to have update details in the XML file as well and show those in the update dialog box, but for now this works. I know it's messy, but it doesn't seem to inflate my program at all, only a few extra megs of memory usage and they get picked up by the garbage collector pretty quickly. Opening the folderBrowserDi alog or openFileDialog uses more memory than this does.

      Comment

      • GaryTexmo
        Recognized Expert Top Contributor
        • Jul 2009
        • 1501

        #4
        Does that work for you? If so then you're probably fine.

        If I had to make changes, I'd make it a little more flexible. What if the format of your XML file changes, for example? Then all your hard-coded array references are now wrong.

        You can get an XmlNode for your updateInfo node and simply do something like this...

        Code:
        // define integers, set to some default (ie, -1)
        
        foreach (XmlNode itemNode in updateInfoNode.ChildNodes)
        {
          switch (itemNode.Name)
          {
            case "currentMajorVersion": // set appropriate int
            // other cases
          }
        }
        
        // Rest of your checks here
        That would just take 'em as they come instead of forcing a specific order.

        Comment

        • HaLo2FrEeEk
          Contributor
          • Feb 2007
          • 404

          #5
          It does work, and I don't plan on ever changing the xml file layout anyway. I only plan on or 2 updates to the program, I just wanted to see if I could get something like this working.

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            Of course, I'll step in with a LINQ solution:
            Code:
            using System;
            using System.Linq;
            using System.Xml.Linq;
            namespace BasicTest
            {
                class Program
                {
                    static void Main(string[] args)
                    {
                        XDocument doc = XDocument.Load(@"c:\dev\update.xml");
                        int majorVersion = Convert.ToInt32(doc.Descendants("currentMajorVersion").First().Value);
                        int minorVersion = Convert.ToInt32(doc.Descendants("currentMinorVersion").First().Value);
                        int revision = Convert.ToInt32(doc.Descendants("currentMinorVersion").First().Value);
                        string download = doc.Descendants("download").First().Value;            
                        Console.ReadKey();
                    }
                }
            }
            To me, it's easier. By the way, this isn't the only way to do it. But this is the most generic. It will find any descendants of the root node (not just children direct children, but recursively), and use the first. It's also a bit easier to see what's going on, because instead of numeric indexes, it uses strings (technically, it's doing a Where() on the underlying IEnumerable, but that's beyond the point).

            Comment

            Working...