XML阅读器无法正常阅读

时间:2017-09-03 22:40:53

标签: c# xml-parsing xmlreader

我正在尝试使用xml阅读器来读取包含xml的URL。我收到一个错误,说字符串为空

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

这是xml的一个示例。我不确定我是否正确使用读卡器

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

请帮助谢谢!

1 个答案:

答案 0 :(得分:0)

以下是打印每个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);
}

https://dotnetfiddle.net/Yek62r

的小提琴