zoukankan      html  css  js  c++  java
  • xmlReader遍历xml文档案例

    class Test
        {
            public static void Main(string[] args)
            {
                string path = "d:\\books.xml";
               

                XmlReader reader = null;
                try
                {
                    // 创建XmlReaderSettings对象
                    XmlReaderSettings settings = new XmlReaderSettings();
                    // 设置XmlReaderSettings对象的属性
                    settings.ProhibitDtd = false;
                    // 使用XmlReaderSettings对象来创建XmlReader对象
                    reader = XmlReader.Create(path, settings);

                    ReadXml(reader);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (reader != null)
                        reader.Close();
                }
            }

            private static void ReadXml(XmlReader reader)
            {
                try
                {
                    // 解析文件,并显示每一个节点
                    while (reader.Read())
                    {
                        switch (reader.NodeType)
                        {
                            case XmlNodeType.Element:
                                if (reader.IsEmptyElement) // 空元素?
                                {
                                    Console.WriteLine("<{0}/>", reader.Name);
                                }
                                else
                                {
                                    Console.Write("<{0}", reader.Name);
                                    if (reader.HasAttributes)   // 属性?
                                    {
                                        while (reader.MoveToNextAttribute())
                                        {
                                            Console.Write(" {0}=\"{1}\"", reader.Name, reader.Value);
                                        }
                                    }
                                    Console.WriteLine(">", reader.Name);
                                }
                                break;
                            case XmlNodeType.Text:
                                Console.WriteLine(reader.Value);
                                break;
                            case XmlNodeType.CDATA:
                                Console.WriteLine("<![CDATA[{0}]]>", reader.Value);
                                break;
                            case XmlNodeType.ProcessingInstruction:
                                Console.WriteLine("<?{0} {1}?>", reader.Name, reader.Value);
                                break;
                            case XmlNodeType.Comment:
                                Console.WriteLine("<!--{0}-->", reader.Value);
                                break;
                            case XmlNodeType.XmlDeclaration:
                                Console.WriteLine("<?xml version='1.0'?>");
                                break;
                            case XmlNodeType.Document:
                                break;
                            case XmlNodeType.DocumentType:
                                Console.WriteLine("<!DOCTYPE {0} [{1}]>", reader.Name, reader.Value);
                                break;
                            case XmlNodeType.EntityReference:
                                Console.WriteLine(reader.Name);
                                break;
                            case XmlNodeType.EndElement:
                                Console.WriteLine("</{0}>", reader.Name);
                                break;
                        }
                    }
                }
                catch (XmlException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }

  • 相关阅读:
    google的开源项目总结
    Google Breakpad 完全解析(二) —— Windows前台实现篇
    Google Breakpad 完全解析(一) —— Windows入门篇
    PVS-Studio静态通用分析规则
    C,C++开源项目中的100个Bugs
    一日一点RakNet(3)--介绍
    C++开源跨平台类库集
    如何实现数组的随机排序?
    JavaScript原型,原型链 ? 有什么特点?
    js的基本数据类型
  • 原文地址:https://www.cnblogs.com/lijinchang/p/1926104.html
Copyright © 2011-2022 走看看