-2

I am trying to use a xml reader to read an url which holds an xml. I am getting an error saying that the strings are null

        public void readEyeData()
        {
            XmlTextReader reader = new XmlTextReader(ExamInfo.eyeURL);

            while (reader.Read() && !(reader.NodeType == XmlNodeType.EndElement && reader.Name == "point"))
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {
                        case "point":
                            var xString = reader.GetAttribute("x");
                            var yString = reader.GetAttribute("y");

                            float x = Int32.Parse(xString);
                            float y = Int32.Parse(yString);
                            view.EyeMove(x, y);
                            break;
                    }
                }
            }
}

And this is a sample of the xml. Im not sure if I am using the reader properly

<studentMovements>
<eyeCoordinates>
<point>
<x>533</x>
<y>521</y>
<ts>522332022.281823</ts>
</point>
<point>
<x>538</x>
<y>521</y>
<ts>522332029.325239</ts>
</point>
<point>
<x>592</x>
<y>529</y>
<ts>522332058.340718</ts>
</point>

Please help thanks!

user8370201
  • 31
  • 1
  • 6

1 Answers1

0

Here's an example that prints each point

foreach (var point in XDocument.Parse(xml).Descendants("point"))
{
    var x = Int32.Parse(point.Element("x").Value);
    var y = Int32.Parse(point.Element("y").Value);
    Console.WriteLine("({0}, {1})", x, y);
}

See the fiddle at https://dotnetfiddle.net/Yek62r

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73