c# - Value does not fall within the expected range exception winrt -


i trying read xml:

    <player>       <id>10101</id>       <name>ricardo ferreira rodrigues</name>       <shirtnumber>1</shirtnumber>       <position>guarda redes</position>       <realteam>5</realteam>    </player> 

and have code :

    private async void loadxml()     {          try         {             storagefolder storagefolder = package.current.installedlocation;             storagefile storagefile = await storagefolder.getfileasync("/users/1101046102/xml/players.xml");              string xml = await fileio.readtextasync(storagefile, windows.storage.streams.unicodeencoding.utf8);              var doc = xdocument.parse(xml);              txtnome.text = (string)doc.root.element("name");             txtshirtnumber.text = (string)doc.root.element("shirtnumber");             txtposition.text = (string)doc.root.element("position");           }          catch (exception ex)         {             xmltextblock.text = ex.message;         }     } 

and getting "value not fall within expected range exception". can tell me why?

your xml document has 2 root elements, both of type "player".

your code trying load xdocument , having trouble because of that.

try wrapping xml elements in container node so...

<players>     <player /> <!-- copied above -->     <player /> <!-- copied above --> </players> 

then need rewrite queries @ child nodes of root element. note since have multiples of them, should try read them collection.

string xml = "<players>"     + "    <player>"     + "        <id>10101</id>"     + "        <name>ricardo ferreira rodrigues</name>"     + "        <shirtnumber>1</shirtnumber>"     + "        <position>guarda redes</position>"     + "        <realteam>5</realteam>"     + "    </player>"     + "    <player>"     + "        <id>10103</id>"     + "        <name>antonio manuel</name>"     + "        <shirtnumber>2</shirtnumber>"     + "        <position>defesa</position>"     + "        <realteam>5</realteam>"     + "    </player>"     + "</players>";         var doc = xdocument.parse(xml);         var rootnode = doc.root;         foreach (var child in rootnode.descendants("player"))         {             // values of child nodes here.         } 

note within loop doesn't make sense assign these values text controls have last 1 in collection written out in manner user can make use of.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -