c# - 我如何使用 XmlTextReader 读取保管箱上的 xml 文件

标签 c# xml xml-parsing dropbox

我试图让我的 winform 应用程序读取存储在 dropbox 上的 xml 文件。但是当它到达

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

然后就不会继续了。但是如果我删除 && (reader.Name == "updater") 然后它会继续但​​它仍然不会读取 "version"和 "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(从下载 按钮),在这里试一试:

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

关于c# - 我如何使用 XmlTextReader 读取保管箱上的 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16758310/

相关文章:

Android 在从 Url 加载图像时没有响应

android - Android XmlResourceParser 的方便使用?

c# - 如何将游戏对象(预制文件夹)显示为图像?

c# - 使用c#直接在类中读取XML文件

java - java中SAX解析器的问题

java - 使用 JDBC ResultSet 构建 XML 文档对象

java - 如何获取具有特定属性值的特定 XML 元素?

c# - 如何防止在 WPF 中的可编辑控件中焦点时使用 "Enter"键关闭窗口

c# - 从旧字典填充新字典

c# - 什么是 C# 相当于 C 中的 strstr()?