0
votes

I am trying to read this xml:

    <player>
      <id>10101</id>
      <name>Ricardo Ferreira Rodrigues</name>
      <shirtnumber>1</shirtnumber>
      <position>Guarda Redes</position>
      <realteam>5</realteam>
   </player>

and i have this 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 i am getting this "Value does not fall within the expected range exception". Someone can tell me why?

2
Did you try adding the xml definition at the top of your xml file? <?xml version="1.0"?> - ps2goat
It is working fine for me,. The values are displaying in TextBlock,, where you are getting exception. - Kumar
ps2goat you won't get exception if you don't put xml version tag. Code runs fine without that tag too - dbw
Can you post your UI code. I believe the text boxes you have in your UI are having some rules applied - dbw

2 Answers

1
votes

Your XML Document is has two root elements, both of type "player".

Your code is trying to load that into an XDocument and it is having trouble because of that.

Try wrapping your xml elements in a container node like so...

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

Then you will need to rewrite your queries to look at the child nodes of the root element. Note that since you have multiples of them, you should probably try to read them into a 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"))
        {
            // do something with the values of the child nodes here.
        }

Note that within the loop it doesn't make sense to assign these values to the text controls you have as only the last one in the collection will get written out in a manner the user can make use of.

-1
votes

the xml I want is :

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

is with this xml i got the error "Value does not fall within the expected range exception".

    <TextBlock Name="txtnome" HorizontalAlignment="Left" Margin="178,275,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
    <TextBlock Name="txtshirtnumber" HorizontalAlignment="Left" Margin="178,300,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
    <TextBlock Name="XmlTextBlock" HorizontalAlignment="Left" Margin="178,250,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>
    <TextBlock Name="txtposition" HorizontalAlignment="Left" Margin="178,325,0,0" TextWrapping="Wrap" VerticalAlignment="Top"/>

This are my textblock