【问题标题】:Using XML To For Application Update Checker将 XML 用于应用程序更新检查器
【发布时间】:2011-04-20 22:09:13
【问题描述】:

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

我正在尝试为我的应用程序制作一个非常简单的更新检查器。一种将解析在线 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.

【问题讨论】:

  • 您使用的是哪个版本的 .NET?

标签: c# xml parsing auto-update


【解决方案1】:

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

首先,您在您的网络主机上存储一个 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。希望这会有所帮助!

【讨论】:

  • 抱歉回复晚了。它按我想要的方式工作。我使用了您的 xml 格式,但对其进行了微小的更改,以便添加更改日志。我还将您添加到“关于”页面,以及指向您网站的链接。
  • 很高兴它为您工作,非常感谢您在“关于”页面上提及!
【解决方案2】:
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 的其他东西上遇到了麻烦,但在你给我的帮助下,我也能够将知识应用到这方面并让它发挥作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 2013-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    相关资源
    最近更新 更多