c#读写XML文件

读XML文档的方法:

using System;
using System.Xml; 

namespace ReadXml
{
    class Class1
    {
        static void Main( string[] args )
        {
            // 创建一个XmlTextReader类的对象并调用Read方法来读取文件
            XmlTextReader textReader  = new XmlTextReader("C:\\books.xml");
            textReader.Read();
            // 节点非空则执行循环体
            while ( textReader.Read() )
            {
                // 读取第一个元素
                textReader.MoveToElement();
                Console.WriteLine("XmlTextReader Properties Test");
                Console.WriteLine("==================="); 

                // 读取该元素的属性并显示在控制台中
                Console.WriteLine("Name:" + textReader.Name);
                Console.WriteLine("Base URI:" + textReader.BaseURI);
                Console.WriteLine("Local Name:" + textReader.LocalName);
                Console.WriteLine("Attribute Count:" + textReader.AttributeCount.ToString());
                Console.WriteLine("Depth:" + textReader.Depth.ToString());
                Console.WriteLine("Line Number:" + textReader.LineNumber.ToString());
                Console.WriteLine("Node Type:" + textReader.NodeType.ToString());
                Console.WriteLine("Attribute Count:" + textReader.Value.ToString());
            }
        }
    }
}
 


XmlTextReader类中有一个很重要的属性-NodeType,通过该属性,我们可以知道其节点的节点类型。而枚举类型XmlNodeType中包含了诸如Attribute、CDATA、Element、Comment、 Document、DocumentType、Entity、ProcessInstruction以及WhiteSpace等的XML项的类型。通过与 XmlNodeType中的元素的比较,我们可以获取相应节点的节点类型并对其完成相关的操作。下面我就给出一个实例,该实例读取每个节点的 NodeType,并根据其节点类型显示其中的内容,同时程序还记录了XML文件中每种节点类型的数目。

using System;
using System.Xml; 

namespace ReadXML
{
    class Class2
    {
        static void Main( string[] args )
        {
            int ws = 0;
            int pi = 0;
            int dc = 0;
            int cc = 0;
            int ac = 0;
            int et = 0;
            int el = 0;
            int xd = 0; 

            XmlTextReader textReader  = new XmlTextReader("C:\\books.xml"); 

            while (textReader.Read())
            {
                XmlNodeType nType = textReader.NodeType;

                // 节点类型为XmlDeclaration
                if (nType == XmlNodeType.XmlDeclaration)
                {
                    Console.WriteLine("Declaration:" + textReader.Name.ToString());
                    xd = xd + 1;
                }          

                // 节点类型为Comment
                if( nType == XmlNodeType.Comment)
                {
                    Console.WriteLine("Comment:" + textReader.Name.ToString());
                    cc = cc + 1;
                }          

                // 节点类型为Attribute
                if( nType == XmlNodeType.Attribute)
                {
                    Console.WriteLine("Attribute:" + textReader.Name.ToString());
                    ac = ac + 1;
                } 

                // 节点类型为Element
                if ( nType == XmlNodeType.Element)
                {
                    Console.WriteLine("Element:" + textReader.Name.ToString());
                    el = el + 1;
                } 

                // 节点类型为Entity
                if ( nType == XmlNodeType.Entity )
                {
                    Console.WriteLine("Entity:" + textReader.Name.ToString());
                    et = et + 1;
                }          

                // 节点类型为Process Instruction
                if( nType == XmlNodeType.ProcessingInstruction )
                {
                    Console.WriteLine("Process Instruction:" + textReader.Name.ToString());
                    pi = pi + 1;
                }      

                // 节点类型为DocumentType
                if( nType == XmlNodeType.DocumentType)
                {
                    Console.WriteLine("DocumentType:" + textReader.Name.ToString());
                    dc = dc + 1;
                } 

                // 节点类型为Whitespace
                if ( nType == XmlNodeType.Whitespace )
                {
                    Console.WriteLine("WhiteSpace:" + textReader.Name.ToString());
                    ws = ws + 1;
                }
            } 

            // 在控制台中显示每种类型的数目
            Console.WriteLine("Total Comments:" + cc.ToString());
            Console.WriteLine("Total Attributes:" + ac.ToString());
            Console.WriteLine("Total Elements:" + el.ToString());
            Console.WriteLine("Total Entity:" + et.ToString());
            Console.WriteLine("Total Process Instructions:" + pi.ToString());
            Console.WriteLine("Total Declaration:" + xd.ToString());
            Console.WriteLine("Total DocumentType:" + dc.ToString());
            Console.WriteLine("Total WhiteSpaces:" + ws.ToString());
        }
    }
}
 


以上,我向大家介绍了如何运用XmlTextReader类的对象来读取XML文档,并根据节点的NodeType属性来取得其节点类型信息。同时XmlReader这个基类还有XmlNodeReader和 XmlValidatingReader等派生类,它们分别是用来读取XML文档的节点和模式的。限于篇幅,这里就不介绍了,读者可以参考有关资料。

写XML文档的方法:

using System;
using System.Xml; 

namespace WriteXML
{
 class Class1
 {
  static void Main( string[] args )
  {
   try
   {
    // 创建XmlTextWriter类的实例对象
    XmlTextWriter textWriter = new XmlTextWriter("C:\\w3sky.xml", null);
    textWriter.Formatting = Formatting.Indented;

    // 开始写过程,调用WriteStartDocument方法
    textWriter.WriteStartDocument(); 

    // 写入说明
    textWriter.WriteComment("First Comment XmlTextWriter Sample Example");
    textWriter.WriteComment("w3sky.xml in root dir");  

    //创建一个节点
    textWriter.WriteStartElement("Administrator");
    textWriter.WriteElementString("Name", "formble");
    textWriter.WriteElementString("site", "w3sky.com");
    textWriter.WriteEndElement();
    


    // 写文档结束,调用WriteEndDocument方法
    textWriter.WriteEndDocument();

    // 关闭textWriter
    textWriter.Close();

   }
   catch(System.Exception e)
   {
    Console.WriteLine(e.ToString());
   }
  }
 }
}


你可能感兴趣的:(xml)