McWxXx
McWxXx

Reputation: 41

Using XML To For Application Update Checker

Before you ask if I've looked at google let me answer yes, I've read page after page. Site after site, and wasn't able to get the information that I needed.

I'm trying to make a very simple update checker for my application. One that will parse the online xml file, and display the data in certain places. As well as being able to parse out the link for the download location(Will not be ftp or anything, but something like a filehost since my hosting plan doesn't allow me to ftp files over 3MBs)

Anyway here is what I got thus far:

XML Code:

<code>
   <Info>
       <Version>2.8.0.0</Version>

       <Link>www.filehost.com</Link>

       <Description>Added New Features To GUI</Description>

   </Info>
</code>

Here's the application code, and what I want it to show and do.

using System;
using System.Windows.Forms;
using System.Xml;

namespace SAM
{
    public partial class UpdateCheck : DevExpress.XtraEditors.XtraForm
    {
        public UpdateCheck()
        {
            InitializeComponent();
            lblCurrentVersion.Text = "Current Version: " + Application.ProductVersion;
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            BringToFront();
        }


        private void BtnChkUpdate_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("http://www.crimson-downloads.com/SAM/UpdateCheck.xml");

        }
    }
}

I'm looking to have the application parse the xml in this way.

<Version>2.8.0.0</Version>  Will change the text for "lblUpdateVersion" like how I got the current version label set in the InitializeComponent();
<Description>Added New Features To GUI</Description> to be parsed out into the "textDescription" Which I can probably do myself.
<Link>www.filehost.com</Link>  Will parse into the button control so when pressed will open up the users default browser and follow the link.

Upvotes: 0

Views: 1907

Answers (2)

McWxXx
McWxXx

Reputation: 41

using System;
using System.Windows.Forms;
using System.Xml;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace SAM
{

    public partial class UpdateCheck : DevExpress.XtraEditors.XtraForm
    {
        public UpdateCheck()
        {
            InitializeComponent();
            lblCurrentVersion.Text = "Current Version:  " + Application.ProductVersion;
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            BringToFront();
        }

        public static string GetWebPage(string URL)
        {
            System.Net.HttpWebRequest Request = (HttpWebRequest)(WebRequest.Create(new Uri(URL)));
            Request.Method = "GET";
            Request.MaximumAutomaticRedirections = 4;
            Request.MaximumResponseHeadersLength = 4;
            Request.ContentLength = 0;

            StreamReader ReadStream = null;
            HttpWebResponse Response = null;
            string ResponseText = string.Empty;

            try
            {
                Response = (HttpWebResponse)(Request.GetResponse());
                Stream ReceiveStream = Response.GetResponseStream();
                ReadStream = new StreamReader(ReceiveStream, System.Text.Encoding.UTF8);
                ResponseText = ReadStream.ReadToEnd();
                Response.Close();
                ReadStream.Close();

            }
            catch (Exception ex)
            {
                ResponseText = string.Empty;
            }

            return ResponseText;
        }

        private void BtnChkUpdate_Click(object sender, EventArgs e)
        {
            System.Xml.XmlDocument VersionInfo = new System.Xml.XmlDocument();
            VersionInfo.LoadXml(GetWebPage("http://www.crimson-downloads.com/SAM/UpdateCheck.xml"));

            lblUpdateVersion.Text = "Latest Version:  " + (VersionInfo.SelectSingleNode("//latestversion").InnerText);

            textDescription.Text = VersionInfo.SelectSingleNode("//description").InnerText;

        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            Process process = new Process();
            // Configure the process using the StartInfo properties.
            process.StartInfo.FileName = "http://www.crimson-downloads.com/SAM/Refresh.htm";
            process.StartInfo.Arguments = "-n";
            process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
            process.Start();
        }
    }
}

Short and simple. Thanks man, was having trouble with something else that uses xml, but with the help you gave me I was able to apply the knowledge to that as well and got it working to.

Upvotes: 1

DWRoelands
DWRoelands

Reputation: 4940

I've done the exact thing this in an application of my own.

First, you store an XML file on your webhost that holds the updater information. Mine is at http://getquitter.com/version.xml and is structured as follows:

<versioninformation>
  <latestversion>1.2.0.0</latestversion> 
  <latestversionurl>http://www.getquitter.com/quitter-1.2.0.zip</latestversionurl> 
  <filename>quitter-1.2.0.zip</filename> 
</versioninformation>

Second, write a method to retrieve that xml from your host:

Public Function GetWebPage(ByVal URL As String) As String
    Dim Request As System.Net.HttpWebRequest = CType(WebRequest.Create(New Uri(URL)), HttpWebRequest)
    With Request
        .Method = "GET"
        .MaximumAutomaticRedirections = 4
        .MaximumResponseHeadersLength = 4
        .ContentLength = 0
    End With

    Dim ReadStream As StreamReader = Nothing
    Dim Response As HttpWebResponse = Nothing
    Dim ResponseText As String = String.Empty

    Try
        Response = CType(Request.GetResponse, HttpWebResponse)
        Dim ReceiveStream As Stream = Response.GetResponseStream
        ReadStream = New StreamReader(ReceiveStream, System.Text.Encoding.UTF8)
        ResponseText = ReadStream.ReadToEnd
        Response.Close()
        ReadStream.Close()

    Catch ex As Exception
        ResponseText = String.Empty
    End Try

    Return ResponseText
End Function

Next, call this method to get the xml and load into an xml document.

Dim VersionInfo As New System.Xml.XmlDocument
VersionInfo.LoadXml(GetWebPage("http://www.getquitter.com/version.xml"))

With version.xml loaded, you can now parse out the individual pieces of data you need to determine whether or not you need to grab the new version.

Dim LatestVersion As New Version(QuitterInfoXML.SelectSingleNode("//latestversion").InnerText)
Dim CurrentVersion As Version = My.Application.Info.Version
If LatestVersion > CurrentVersion Then
     ''download the new version using the Url in the xml
End If

This is how my application does it. You can download the source code if you like (it's an open source application) if you'd like to use it as a model. It's at http://quitter.codeplex.com. Hope this helps!

Upvotes: 2

Related Questions