How to read xml file in Dropbox using XmlTextReader

I am trying to get my winform application to read the xml file stored in dropbox. But when he gets to

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

Then it will not continue. but if I delete && (reader.Name == "updater"), then it continues, but it still does not 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 update.xml in Dropbox with an XmlTextReader, but I just never get any values ​​back!

I tried posting messages to show me reader.Name, but it returns "html" instead of "updater".

No need to be an XmlTextReader. While the solution works, then its fine: D

Any help would 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!"); } } 
+4
source share
1 answer

Well, going to the provided Url does not give you an XML file, as you might expect, it actually provides you content through the Dropbox shell. So when you read, you click on the HTML page. You can double check this by setting a breakpoint on elementName for each iteration.

This is the correct URL https://dl.dropboxusercontent.com/s/25gcpllsqsj1qrq/update.xml?token_hash=AAFnI4T9nSCGeVgDKLm7YB79dbjNtoYkAj_mChxVDZL0AQ&dl=1 (get from the Download button)

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

This is an alternative way to load content into an XDocument and query it using 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 a valid XDocument) } 
+4
source

Source: https://habr.com/ru/post/1482801/


All Articles