4
我想分析以下xml以獲取richTextBox1,以便顯示'John Smith','35','Jpeg'。「根數據無效,第1行,第1位」解析XML
<?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。我怎樣才能使這個工作,任何輸入是非常感激。
謝謝你們犯了同樣的錯誤,它幫了我很多:-) – isHuman