C#解析XML字符串

来源:互联网 发布:中兴n760软件下载 编辑:程序博客网 时间:2024/05/22 02:15
StringBuilder output = new StringBuilder();String xmlString =    @"<bookstore>        <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>            <title>The Autobiography of Benjamin Franklin</title>            <author>                <first-name>Benjamin</first-name>                <last-name>Franklin</last-name>            </author>            <price>8.99</price>        </book>    </bookstore>";using (XmlReader reader = XmlReader.Create(new StringReader(xmlString))){    reader.ReadToFollowing("book");    reader.MoveToFirstAttribute();    string genre = reader.Value;    //结果:'autobiography'    output.AppendLine("The genre value: " + genre);    reader.ReadToFollowing("title");    output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString());    //结果:The Autobiography of Benjamin Franklin}OutputTextBlock.Text = output.ToString();