簡體   English   中英

如何使用XmlTextReader讀取Dropbox上的xml文件

[英]How do i read xml file on dropbox with XmlTextReader

我試圖讓我的winform應用程序讀取存儲在dropbox上的xml文件。 但是當它到達時

if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "updater"))

然后它不會繼續。 但如果我刪除&&(reader.Name ==“updater”)然后繼續但它仍然沒有讀取“版本”和“url”的值。

鏈接到update.xml: https ://www.dropbox.com/s/25gcpllsqsj1qrq/update.xml

下面是我項目中的C#代碼。 我試圖用XmlTextReader讀取dropbox上的update.xml,但我從來沒有得到任何值!

我試着放置消息框來向我顯示reader.Name,但它返回“html”而不是“updater”。

它不必是XmlTextReader。 只要解決方案有效,那就好了:D

任何幫助將不勝感激。

private void checkForUpdatesToolStripMenuItem_Click(object sender, EventArgs e)
{
    Version newVersion = null;
    string url = "";
    XmlTextReader reader = null;

    try
    {
        string xmlURL = "https://www.dropbox.com/s/25gcpllsqsj1qrq/update.xml";
        reader = new XmlTextReader(xmlURL);
        reader.MoveToContent();
        string elementName = "";
        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "updater"))
        {
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element) elementName = reader.Name;
                else
                {
                    if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
                    {
                        switch (elementName)
                        {
                            case "version":
                                newVersion = new Version(reader.Value);
                                break;
                            case "url":
                                url = reader.Value;
                                break;
                        }
                    }
                }
            }
        }
    }
    catch (Exception)
    {
    }
    finally
    {
        if (reader != null) reader.Close();
    }

    Version curVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;  
    if (curVersion.CompareTo(newVersion) < 0)
    { 
        string title = "New version detected.";
        string question = "Download the new version?";
        if (DialogResult.Yes == MessageBox.Show(this, question, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
        {
            System.Diagnostics.Process.Start(url);
        }
    }
    else
    { 
        MessageBox.Show("The program is up to date!");
    }
}

好吧,轉到提供的Url並沒有像你期望的那樣提供XML文件,它實際上是通過Dropbox包裝器為你提供內容。 因此,當您正在閱讀時,您實際上正在訪問HTML頁面。 您可以通過在每次迭代的elementName上放置一個斷點來仔細檢查這一點。

這是正確的URL https://dl.dropboxusercontent.com/s/25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1 (從“ Download按鈕獲取),這里嘗試一下:

string xmlURL = "https://dl.dropboxusercontent.com/s/25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1";

這是將內容加載到XDocument並使用LINQ進一步查詢的另一種方法:

string url = "https://dl.dropboxusercontent.com/s/25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1";
WebRequest request = HttpWebRequest.Create(url);

using (WebResponse response = request.GetResponse())
using (Stream stream = response.GetResponseStream())
{
    var doc = XDocument.Load(stream);

    // do your magic (now it's a valid XDocument)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM