[英]C# XML Parsing - Data at the root level is invalid. Line 1, position 1
[英]"Data at the root level is invalid. Line 1, position 1" When Parsing XML
我想解析以下 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 行,position 1。 我怎樣才能讓它工作,非常感謝任何輸入。
StringReader 讀取文字字符串。 您正在嘗試解析字符串“Gamer.xml”,而不是文件的內容。
請改用 StreamReader。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.