根级别的数据无效.第一行,位置1"解析XML时

本文关键字:位置 quot XML 解析 一行 数据 无效 | 更新日期: 2023-09-27 18:05:46

我想解析下面的xml获取richTextBox1所以显示'John Smith', '35', 'Jpeg'。

<?xml version="1.0" encoding="utf-8" ?> 
- <Games>
- <Gamer Name="John Smith" Age="35" Win%="5.33502797236373">
   <Picture-id>Jpeg</Picture-id> 
   <Game>300</Game> 
  </Gamer>
</Games>

我使用了以下代码来尝试做到这一点-

StringBuilder output = new StringBuilder();
String xmlString = @"Gamer.xml";
// Create an XmlReader
using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
{
    reader.ReadToFollowing("Gamer");
    reader.MoveToFirstAttribute();
    string genre = reader.Value;
    output.AppendLine("Name" + "Age");
    reader.ReadToFollowing("Picture-id");
    output.AppendLine(reader.ReadElementContentAsString());
}
richTextBox1.Text = output.ToString();

由于某些原因,当我执行时,它返回了错误-'根级别的数据无效。1号线,1号位置。我怎么能得到这个工作,任何输入是非常感谢。

根级别的数据无效.第一行,位置1"解析XML时

StringReader读取一个字面值字符串。您正在尝试解析字符串"Gamer.xml",而不是文件的内容。