【发布时间】:2011-10-28 23:51:44
【问题描述】:
我想解析以下 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。我怎样才能让它工作,非常感谢任何输入。
【问题讨论】:
-
感谢您犯了同样的错误,这对我很有帮助:-)