c# - Delving into the world of XML (Windows Phone) Error I dont understand (The ' ' character, hexadecimal value 0x20, cannot be included in a name.) -


so starting learn how use xml data within app , decided use free data cannot life of me working code far. (i have done few apps static data before hey apps designed use web right? :p)

public partial class mainpage : phoneapplicationpage {     list<xmlitem> xmlitems = new list<xmlitem>();     // constructor     public mainpage()     {         initializecomponent();         loadxmlitems("http://hatrafficinfo.dft.gov.uk/feeds/datex/england/currentroadworks/content.xml");         test();     }      public void test()     {         foreach (xmlitem item in xmlitems)         {             testing.text = item.title;         }     }      public void loadxmlitems(string xmlurl)     {         webclient client = new webclient();          client.openreadcompleted += (sender, e) =>         {             if (e.error != null)                 return;              stream str = e.result;             xdocument xdoc = xdocument.load(str);              ***xmlitems = (from item in xdoc.descendants("situation id")                                                 select new xmlitem()                                                 {                                                     title = item.element("impactontraffic").value,                                                     description = item.element("trafficrestrictiontype").value                                                 }).tolist();***             // close             str.close();              // add results list             xmlitems.clear();             foreach (xmlitem item in xmlitems)             {                 xmlitems.add(item);             }         };         client.openreadasync(new uri(xmlurl, urikind.absolute));     } } 

i trying learn how @ moment intrigued how (i know there many ways atm way seems easiest) don't error atm. (the bit in * says error is)

i know display function atm not great (as show last item) testing now.

to may seem easy, learner not easy me yet.

the error in picture form: (it seems cant post images :/)

thanks in advance help

edit: answer below fixed error :d still nothing coming up. "think" it's because of xml layout , amount of descendants has (cant work out need being noob @ xml , pulling web data source)

maybe starting complicated :/

still help/tips on how pull elements feed (as there in descendants) correctly , store them great :d

edit2: have working (in crude way) still :d

thanks adam maras!

the last issue double listing. (adding list, add list causing null exception) using 1 list within method solved issue, (probably not best way of doing works now) , allowed me add results listbox until spend time working out how use listbox.itemtemplate & datatemplate make more appealing. (seems easy enough now...)

thanks again!!!

from item in xdoc.descendants("situation id") //                                      ^ 

xml tag names can't contain spaces. looking @ xml, want "situation" match <situation> elements.


after looking @ edit , further reviewing xml, figured out problem is. if @ root element of document:

<d2logicalmodel xmlns="http://datex2.eu/schema/1_0/1_0" modelbaseversion="1.0"> 

you'll see has default namespace applied. easiest solution problem first namespsace root element:

var ns = xdoc.root.name.namespace; 

and apply wherever you're using string identify element or attribute name:

from item in xdoc.descendants(ns + "situation") // ... item.element(ns + "impactontraffic").value item.element(ns + "trafficrestrictiontype").value 

one more thing: <impactontraffic> , <trafficrestrictiontype> aren't direct children of <situation> element, you'll need change code well:

title = items.descendants(ns + "impactontraffic").single().value, description = item.descendants(ns + "trafficrestrictiontype").single().value 

Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -