我如何与Dropbox的XmlTextReader的读取XML文件 [英] How do i read xml file on dropbox with XmlTextReader

查看:96
本文介绍了我如何与Dropbox的XmlTextReader的读取XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的WinForm应用程序读取存储在Dropbox的XML文件。但是,当它到达

  IF((reader.NodeType == XmlNodeType.Element)及及(reader.Name = =更新))

然后,它不会继续。但如果我删除和放大器;&安培; (reader.Name ==更新),然后继续,但它仍然不读的版本和URL的值



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



下面是在我的项目我的C#代码。我想读与Dropbox的XmlTextReader的在update.xml,但我从来没有得到任何的价值了!



我tryed放置提示消息框来显示我的读者。名称,但它返回HTML,而不是更新。



这不必要必须是XmlTextReader的。只要解决方案有效,那么它的精致:D



任何帮助将不胜感激。



私人无效checkForUpdatesToolStripMenuItem_Click(对象发件人,EventArgs五)
{
版NEWVERSION = NULL;
字符串的URL =;
XmlTextReader的读者= NULL;


{
串XMLURL =https://www.dropbox.com/s/25gcpllsqsj1qrq/update.xml;
=读者XmlTextReader的新(美语);
reader.MoveToContent();
字符串的ElementName =;
如果((reader.NodeType == XmlNodeType.Element)及及(reader.Name ==更新))
{
,而(reader.Read())
{
如果(reader.NodeType == XmlNodeType.Element)的ElementName = reader.Name;
,否则
{
如果((reader.NodeType == XmlNodeType.Text)及及(reader.HasValue))
{
开关(的ElementName)
{
案版本:
= NEWVERSION新版本(reader.Value);
中断;
案URL:
URL = reader.Value;
中断;
}
}
}
}
}
}
赶上(例外)
{
}
终于
{
如果reader.Close()(读者!= NULL);
}

版curVersion = System.Reflection.Assembly.GetExecutingAssembly()的GetName()版本。;
如果(curVersion.CompareTo(NEWVERSION)小于0)。新版本检测
{
字符串标题=;
串的问题=下载新版本吗?
如果(DialogResult.Yes == MessageBox.Show(这一点,问题,标题,MessageBoxButtons.YesNo,MessageBoxIcon.Question))
{
System.Diagnostics.Process.Start(URL);
}
}
,否则
{
MessageBox.Show(该计划是最新的!);
}
}


解决方案

好,将提供不给你,你可能会想到,它实际上给你通过Dropbox的包装内容的XML文件的URL。所以,当你做你读,你实际上击中 HTML 页。通过把一个破发点上的的ElementName 每次迭代你可以仔细检查这一点。



这是正确的网址 https://dl.dropboxusercontent.com/s/25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1 (从得到它下载按钮),在这里试试这个:

 字符串XMLURL =https://开头DL ?.dropboxusercontent.com /秒/ 25gcpllsqsj1qrq / update.xml token_hash = AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&放大器;(DL)= 1; 

这是将内容加载到的XDocument 进一步查询其与LINQ:

 字符串的URL =https://dl.dropboxusercontent.com/s /25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1; 
的WebRequest请求= HttpWebRequest.Create(URL);

使用(WebResponse的响应= request.GetResponse())
使用(流流= response.GetResponseStream())
{
VAR DOC = XDocument.Load(流);

//做你的魔法(现在它是一个有效的XDocument)
}


I am trying to make my winform application read an xml file stored on dropbox. But when it gets to

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

Then it wont continue. but if i remove && (reader.Name == "updater") then it continues but it still dont read the value of "version" and "url".

Link to update.xml: https://www.dropbox.com/s/25gcpllsqsj1qrq/update.xml

Below is my C# code in my project. I am trying to read the update.xml on dropbox with XmlTextReader, but i just never get any of the values back!

I tryed to place messageboxes to show me reader.Name, but it returns "html" instead of "updater".

It dont necessary have to be XmlTextReader. As long as the solution works, then its fine :D

Any help will be appreciated.

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!");
    }
}

解决方案

Well, going to the Url provided doesn't give you the XML file as you might expect, it's actually giving you the content through a Dropbox wrapper. So when you're doing your read, you're actually hitting the HTML page. You can double-check this by putting a break point on the elementName for each iteration.

This is the correct URL https://dl.dropboxusercontent.com/s/25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1 (got it from the Download button), here give this a try:

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

This is an alternative way to load the contents to a XDocument and further query it with 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)
}

这篇关于我如何与Dropbox的XmlTextReader的读取XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆