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