开发者

"Data at the root level is invalid. Line 1, position 1" When Parsing XML

I am wanting to parse the following xml to get richTextBox1 so show '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>

I have used the following code to try and do this -

StringBuilder output = new StringBuilder();

String xmlString = @"Gamer.xml";

// Create an XmlReader
using (XmlReader reader = XmlReader.Crea开发者_JAVA技巧te(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();

For some reason when I execute it brings back the error - 'Data at the root level is invalid. Line 1, position 1.' How can I get this to work, any input is greatly appreciated.


StringReader reads a literal string. You are trying to parse the string "Gamer.xml", not the contents of the file.

Use StreamReader instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜