Using XML To For Application Update Checker

本文关键字:Update Checker Application For XML To Using | 更新日期: 2023-09-27 17:51:25

在你问我是否看过谷歌之前,让我回答是的,我已经读了一页又一页了。一个又一个网站,无法获得我需要的信息。

我正在尝试为我的应用程序制作一个非常简单的更新检查器。它将解析在线xml文件,并在某些地方显示数据。以及能够解析出下载位置的链接(不会是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.

Using XML To For Application Update Checker

我在自己的应用程序中做了同样的事情。

首先,在Web主机上存储一个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.希望这能有所帮助!

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的东西上遇到了麻烦,但在你的帮助下,我也能够将知识应用于此,并使其发挥作用。