XML文档应用C#篇(二)

来源:互联网 发布:恶意锁屏软件下载 编辑:程序博客网 时间:2024/06/04 00:48

使用XMLReader读取XML文件

应用实例

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;                   //记得添加这个系统引用

 

namespace XmlWriterDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"C:/Documents and Settings/张健和/桌面/Roy.xml";

            string oldpath = @"C:/Documents and Settings/张健和/桌面/Royson.xml";

            //尝试读取该XML文件

            try

            {

                XmlReaderSettings setting = new XmlReaderSettings();//创建新的设置实例

                setting.ConformanceLevel = ConformanceLevel.Fragment;       //一致性级别设置为格式良好的XML文件

                setting.IgnoreWhitespace = true;//忽略无关紧要的空白

                setting.IgnoreComments = true;//是否忽略注释

                XmlReader myReader = XmlReader.Create(oldpath, setting);

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

 

读取XML元素值

xmlReader常用方法

方法

说明

isStartElement

检查当前节点是否是开始标记或空的元素标记

ReadStartElement

检查当前节点是否为元素并将读取器推进到下一个节点

ReadEndElement

检查当前节点是否为结束标记并将读取器推进到下一个节点

ReadElementString

读取纯文本元素

ReadToDescendant

XmlReader前进到具有指定名称的下一个子代元素

ReadToNextSibling

XMLReader前进到具有指定名称的下一个同辈元素

IsEmptyElement

检查当前元素是否包含空的元素标记

应用实例1

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;                   //记得添加这个系统引用

 

namespace XmlWriterDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"C:/Documents and Settings/张健和/桌面/Roy.xml";

            string oldpath = @"C:/Documents and Settings/张健和/桌面/Royson.xml";        //XML文件必须预先创建。

            //尝试读取该XML文件

            try

            {

                XmlReader myReader = XmlReader.Create(oldpath);

                myReader.ReadStartElement("Roy");

 

                myReader.ReadStartElement("name");

                Console.Write("name是:");

                Console.WriteLine(myReader.ReadString());

                myReader.ReadEndElement();             //结束NAME节点

 

                myReader.ReadStartElement("title");

                Console.Write("title是:");

                Console.WriteLine(myReader.ReadString());

                myReader.ReadEndElement();

 

                myReader.ReadStartElement("body");

                Console.Write("body是:");

                Console.WriteLine(myReader.ReadString());

                myReader.ReadEndElement();

 

                myReader.ReadEndElement();                  //结束ROY节点

;

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

应用实例2

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;                   //记得添加这个系统引用

 

namespace XmlWriterDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"C:/Documents and Settings/张健和/桌面/Roy.xml";

            string oldpath = @"C:/Documents and Settings/张健和/桌面/Royson.xml";        //XML文件必须预先创建。

            //尝试读取该XML文件

            try

            {

                XmlReader myReader = XmlReader.Create(oldpath);

                //myReader.Read()为真是,读取XML文件内的内容

                while (myReader.Read())        //Read读取下一个节点。。

                {

                    if (myReader.IsStartElement())

                    {

                        if (myReader.IsEmptyElement)         //当前节点是否为空节点

                        {

                            Console.WriteLine("<{0}/>", myReader.Name);        //当前节点的限定名

                        }

                        else

                        {

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

                            myReader.Read();          //读取了下一个节点

                            if (myReader.IsStartElement())

                            {

                                Console.Write("/r/n<{0}>", myReader.Name);

                            }

                            Console.Write(myReader.ReadString());

                            Console.WriteLine("</{0}>", myReader.Name);

                        }

                    }

                }

                Console.WriteLine("</Roy>");

;

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

 

读取XML文件的属性值

应用实例1

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;                   //记得添加这个系统引用

 

namespace XmlWriterDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"C:/Documents and Settings/张健和/桌面/Roy.xml";

            string oldpath = @"C:/Documents and Settings/张健和/桌面/Royson.xml";        //XML文件必须预先创建。

            //尝试读取该XML文件

            try

            {

                XmlReader myReader = XmlReader.Create(oldpath);

                myReader.IsStartElement("Roy");                        //跳到有指定索引的节点

                Console.WriteLine("日期为");

                Console.WriteLine(myReader.GetAttribute("date"));         //获取当前节点的属性值

                Console.WriteLine("性别");

                Console.WriteLine(myReader.GetAttribute("Sex"));

                Console.WriteLine("国籍");

                Console.WriteLine(myReader.GetAttribute("nation"));    

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

 

应用实例2

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml;                   //记得添加这个系统引用

 

namespace XmlWriterDemo

{

    class Program

    {

        static void Main(string[] args)

        {

            string path = @"C:/Documents and Settings/张健和/桌面/Roy.xml";

            string oldpath = @"C:/Documents and Settings/张健和/桌面/Royson.xml";        //XML文件必须预先创建。

            //尝试读取该XML文件

            try

            {

                XmlReader myReader = XmlReader.Create(oldpath);

                while (myReader.Read())          //读入节点

                {

                    if (myReader.HasAttributes)          //检查当前节点是否含有任何属性

                    {

                        Console.WriteLine(myReader.Name + "的属性:");

                        while (myReader.MoveToNextAttribute())        //移动到元素的下一个属性

                        {

                            Console.WriteLine("{0}={1}", myReader.Name, myReader.Value);

                        }

                        myReader.MoveToElement();        //移动到下一个元素

                    }

                }

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

 

 

原创粉丝点击