2011-04-20 56 views
0

在你問我是否看過谷歌之前讓我回答是的,我已閱讀一頁一頁。網站站點之後,並且無法獲取我需要的信息。使用XML爲應用程序更新檢查器

我想爲我的應用程序做一個非常簡單的更新檢查器。一個將解析在線XML文件,並顯示在某些地方的數據。以及能夠解析出鏈接的下載位置(不會是FTP或任何東西,但像一個文件主機,因爲我的託管計劃不允許我的FTP文件超過3MBs)

無論如何,這裏是什麼我迄今:

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

您正在使用哪個版本的.NET? – 2011-04-20 22:22:32

+0

你的問題是什麼? – 2011-04-20 22:39:31

回答

2

我已經在我自己的應用程序中完成了這件事。

首先,您將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。希望這可以幫助!

+0

抱歉回覆遲了。它以我想要的方式工作。我使用了你的xml格式,但對它做了一個小改動,所以我可以添加一個更改日誌。我還將您添加到關於頁面的鏈接以及指向您網站的鏈接。 – McWxXx 2011-04-25 04:16:40

+0

很高興爲您效力,非常感謝您在「關於」頁面提及! – DWRoelands 2011-04-26 14:39:41

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的其他東西時遇到了麻煩,但在你給我的幫助下,我能夠將知識應用於此,並使其工作。

相關問題