使用XML To Application Update Checker

时间:2011-04-20 22:09:14

标签: c# xml parsing auto-update

在你问我是否看过谷歌之前让我回答是的,我已经逐页阅读了。站点之后的站点,并且无法获得我需要的信息。

我正在尝试为我的应用程序创建一个非常简单的更新检查程序。一个将解析在线xml文件,并在某些地方显示数据。除了能够解析下载位置的链接(不会是ftp或任何东西,但像文件主机,因为我的托管计划不允许我ftp文件超过3MB)

无论如何,这是我到目前为止所得到的:

XML代码:

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

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

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

   </Info>
</code>

这是应用程序代码,以及我希望它显示和执行的操作。

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

        }
    }
}

我希望应用程序以这种方式解析xml。

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

2 个答案:

答案 0 :(得分:2)

我已经在我自己的应用程序中完成了这件事。

首先,在webhost上存储一个包含更新程序信息的XML文件。我在http://getquitter.com/version.xml,结构如下:

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

其次,编写一个从主机中检索xml的方法:

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

接下来,调用此方法获取xml并加载到xml文档中。

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

加载version.xml后,您现在可以解析所需的各个数据,以确定是否需要获取新版本。

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

这就是我的应用程序的功能。如果您愿意,可以下载源代码(它是一个开源应用程序),如果您想将其用作模型。它在http://quitter.codeplex.com。希望这有帮助!

答案 1 :(得分:1)

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

简短而简单。谢谢男人,在使用xml的其他东西时遇到了麻烦,但是在你给我的帮助下,我能够将这些知识应用到那个并且让它工作。