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();
C#解析XML字符串
最新推荐文章于 2023-12-27 19:27:12 发布
本文介绍了一个使用C#进行XML数据读取和解析的例子。通过创建一个String类型的XML字符串,利用XmlReader来读取特定元素的内容及属性值。演示了如何获取书籍的类型和标题等关键信息。
508

被折叠的 条评论
为什么被折叠?



