码迷,mamicode.com
首页 > 其他好文 > 详细

如何解析XML文件或字符串

时间:2014-06-16 22:59:13      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:class   blog   code   使用   文件   数据   


1 引用XML文件
2 使用XMLReader解析文本字符串
3 使用XMLReader方法读取XML数据

具体代码实现如下:
//初始化一个XML字符串
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>";

ParseXml(xmlString);


static void ParseXml(string xmlString)
{
             
            //创建一个StringBuilder对象用来保存从XML文件中解析出来的文本内容
            StringBuilder output = new StringBuilder();

            创建一个XML读取器
            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                reader.ReadToFollowing("book");
                reader.MoveToFirstAttribute();
                string genre = reader.Value;
                output.AppendLine("The genre value: " + genre);

                reader.ReadToFollowing("title");
                output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString());
             }

            
             Console.WriteLine(output);
}


如何解析XML文件或字符串,布布扣,bubuko.com

如何解析XML文件或字符串

标签:class   blog   code   使用   文件   数据   

原文地址:http://blog.csdn.net/u011685627/article/details/31114409

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!