XmlReader读取xml文档

private static void ReadXml(XmlReader reader)

{

    try

    {

         while (reader.Read())

                {

                    switch (reader.NodeType)

                    {

                        case XmlNodeType.Element:

                            if (reader.IsEmptyElement)      //空元素?

                            {

                                Console.Write("<{0}", reader.Name);

                                if (reader.HasAttributes)   //属性?

                                {

                                    while (reader.MoveToNextAttribute())

                                    {

                                        Console.Write(" {0}=\"{1}\"", reader.Name, reader.Value);

                                    }

                                }

                                Console.WriteLine("/>");

                            }

                            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);

    }

}

 

你可能感兴趣的:(reader)