xml文档应用C#篇

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

XML简介:

XML,全称Extensible Markup Language,中文名称是可扩展标记语言。用XML语言编写的文件称为XML文件,由于其平台无关性,被广泛应用于数据交换、各种系统的配置等方面。

 

使用XMLWriter创建XML文件

属性

初始值

CheckCharacters

True

CloseOutput

False

ConformanceLevel

ConformanceLevel.Document

Encoding

Encoding.UTF8

Indent(指示是否缩进元素)

False

IndentChars(指示缩进时用的字符)

两个空格

NewLineChars

/r/n(回车符,换行符)

NewLineHandlin

NewHandling.Replace

NewLineOnAttributtes

False

OmitXmlDeclaration

False

应用实例:

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

            //尝试读取该XML文件

            try

            {

                XmlWriterSettings mysetting = new XmlWriterSettings();       //记得创建XML实例,记得。

                mysetting.Indent = true;

                mysetting.IndentChars = ("  ");

                XmlWriter mywriter = XmlWriter.Create(path, mysetting);

 

                //输入XML数据

                mywriter.WriteStartElement("Roy");           //写入一个元素

                mywriter.WriteElementString("name", "zhang");         //写入另一个元素

                mywriter.WriteEndElement();//结束写入

                mywriter.Flush();

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

 

写入XML属性值和写入XML元素值

     使用WriterAttributes方法可以复制在提供XmlReader对象的当前位置上发现的所有属性。WriterAttributes行为取决于读取器当前所处的节点类型。下表介绍对每种节点类型调用WriterAttributes的结果。如果读取器所处节点没有在下表列出,WriterAttributes没有任何操作。

节点类型

WriterAttributes行为

Attribute

编写当前属性,然后编写其他属性,直到元素结束标记为止

Element

编写该元素包含的所有属性

XML Declaration

编写声明中的所有属性

 

应用实例:

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

            //尝试读取该XML文件

            try

            {

                XmlWriterSettings mysetting = new XmlWriterSettings();       //记得创建XML实例,记得。

                mysetting.Indent = true;

                mysetting.IndentChars = ("  ");

                XmlWriter mywriter = XmlWriter.Create(path, mysetting);

 

                //输入XML数据

                mywriter.WriteStartElement("Roy");           //写入一个元素,引号内为元素的集合名

                mywriter.WriteStartAttribute("nation");         //根元素属性

                mywriter.WriteValue("Chinese");                 //写入根元素属性值

                mywriter.WriteEndAttribute();                   //结束属性的写入

                mywriter.WriteAttributeString("Sex", "Male");//写入属性值

                mywriter.WriteElementString("name", "zhang");         //写入另一个元素

                mywriter.WriteElementString("Grade", "大二");

                mywriter.WriteEndElement();//结束写入

                mywriter.Flush();

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

WriteStartElement方法是WriteAttributeString方法的更高级版本。它可以使用多个方法调用编写属性值。

 

WriteElementString,WriteStartElementWriteNode方法用于编写元素节点。WriteElementString用于编写整个元素节点,包括字符串值。以下演示了WriteElementString的应用。

WriteStartElementWriteElementString方法更高级的版本。它可以使用多个方法调用编写元素值。例如,可以调用WriteValue来编写类型化的值,调用WriteCharEntity来编写字符实体,调用WriteAttributeString来编写属性,也可以编写子元素。通过调用WriteEndElementWriteFullEndElement方法来关闭该元素。

使用WriteNode方法可以复制XmlReaderXPathNavigator对象的当前位置上发现的整个元素节点。在调用时,会将源对象中的所有内容复制到XmlWriter实例中。以下演示了WriteNode方法的用法。

应用实例:

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

            {

                XmlWriterSettings mysetting = new XmlWriterSettings();       //记得创建XML实例,记得。

                mysetting.Indent = true;

                mysetting.IndentChars = ("  ");

 

                XmlReader myXmlReader = XmlReader.Create(oldpath);          //输入XML数据;

                myXmlReader.ReadToDescendant("Roy");

 

                //创建XMLwriter实例

                XmlWriter myWriter = XmlWriter.Create(path);

                myWriter.WriteNode(myXmlReader, true);

                myWriter.Flush();

            }

            catch (System.Exception e)

            {

                Console.WriteLine(e.Message);

            }

            Console.ReadLine();

        }

    }

}

 

 

原创粉丝点击