liye

liye

博客园 首页 新随笔 联系 订阅 管理

XML是The Extensible Markup Language(可扩展标识语言)的简写。目前推荐遵循的是W3C组织于2000年10月6日发布的XML1.0版本,参考(www.w3.org/TR/2000/REC-XML-20001006)。和HTML一样,XML同样来源于SGML,但XML是一种能定义其他语言的语。XML最初设计的目的是弥补HTML的不足,以强大的扩展性满足网络信息发布的需要,后来逐渐用于网络数据的转换和描述。

1.XML(extensible markup language)以简单文本格式存储数据的方式。只能有一个根元素。
2.元素:element;属性:Attribute;命名空间:xmlns;验证XML文档:DTD,schema->XSD(Schema Definition language)
3.xml声明
<?xml version="1.0"?>
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="GBK"?>
4.xml注释
<!--注释内容-->
5.XSD在.net中也称作XML架构,可以规定元素和属性的数据类型,以xsd为文件后缀。
命名空间:http://www.w3.org/2001/XMLSchema
根结点:<schema>...</schema>
元素:element
出现的次数:choice
最大出现次数:maxOccurs
出现次数不限:unbounded
复杂类型:complexType
6.文档对象模型
基于DOM(Document Object Model)->XmlDocument
基于流SAX(Simple API for AML)->XmlReader,XmlWriter
7.DOM以树结构方式描述XML
8.XmlReader只向前,非缓存的读取器,没有灵活的导航性能
用法:XmlTextReader newReader=newXmlTextReader("filename");
        newReader.Close();
9.XmlWriter为生成XML流提供“只向前,非缓存”方式的抽象类
用法:XmlTextWriter写xml文件的过程和文件系统写数据的过程一样
       XmlTextWriter bookwriter=new XmlTextWriter("c:\\book.xml",Encoding.Default);
10.XPath是xml文档查询语言,用于从xml文档中选择节点集。用法:XmlNode curNode=new XmlNode(); XmlNode Node=curnode.SelectSingleNode("Xpath语句");或者XmlNodeList nodelist = curnode.SelectNodes("Xpath语句");

————————————————————————————————————————————  

xsd文件先定义xml框架,可以为普通的xml文档引用:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="schemaBook2"
           targetNamespace="http://tempuri.org/schemaBook2.xsd"
           elementFormDefault="qualified" xmlns="http://tempuri.org/schemaBook2.xsd"
           xmlns:mstns="http://tempuri.org/schemaBook2.xsd"
           xmlns:xs="http://www.w3.org/2001/XMLSchema " >//引用命名空间,将不同词汇表中的元素引用到该xml文档

<xs:complexType name="abook">
    <xs:sequence>
      <xs:element name="author" type="xs:string">
      </xs:element>
      <xs:element name="price" type="xs:decimal">
      </xs:element>
    </xs:sequence>
    <xs:attribute name="title" type="xs:string">
    </xs:attribute>
</xs:complexType>

<xs:complexType name="manyBooks">
    <xs:choice maxOccurs="unbounded">
      <xs:element name="book" type="mstns:abook">
      </xs:element>
    </xs:choice>
</xs:complexType>

<xs:element name="store" type="mstns:manyBooks">
</xs:element>

</xs:schema>
<!--第三使用此架构的xml文档使用的引用-->
<!--第四也可以取值:unqualified-->
<!--第五行表示定义当前代码段的命名空间-->
<!--第六把本命名空间,mstns是本命名空间的缩写-->
<!--第七行命名空间,不是网站,xs是命名空间的缩写,其中定义了一些基本的数据类型-->

在普通xml文档中引用上面的xsd框架:

<?xml version="1.0" encoding="utf-8" ?>
<store xmlns="http://tempuri.org/schemaBook2.xsd" >
<book title ="C#学习">
    <author >王涛</author>
    <price >300</price>
</book>
<book title =".net">
    <author >老谭</author>
    <price >223</price>
</book>
</store>

————————————————————————————————————————————

递归遍历输出xml文件所有节点,基于DOm

        /// <summary>
        /// 递归方法遍历xml文档
        /// </summary>
        /// <param name="curnode">每次遍历的根结点</param>
        /// <param name="padding">每次遍历输出地空格数</param>
        private void showinlistbox(XmlNode curnode,int padding)
        {
            if (curnode == null)
            {
                return;
            }
            if (curnode is XmlElement)//元素类型结点
            {
                string attribute = curnode.Name;
                if (curnode.Attributes.Count != 0)//判断该元素是不是有属性类型的结点
                {
                    attribute += ":" + curnode.Attributes[0].Value.ToString();
                }
                attribute =attribute.PadLeft(attribute.Length + padding);//这种在前面加空格的方法不错
                this.listBox1.Items.Add(attribute);
                if (curnode.HasChildNodes)//有子节点就加两个空格
                {
                    showinlistbox(curnode.FirstChild,padding +2);
                }
                if (curnode .NextSibling !=null )//兄弟结点不加空格
                {
                    showinlistbox(curnode.NextSibling,padding);
                }
            }
            else if (curnode is XmlText)//值类型结点,没有兄弟结点和子结点
            {
                string text =curnode.Value.ToString();
                text=text.PadLeft(text .Length +padding);
                this.listBox1.Items.Add(text );
            }
            else if (curnode is XmlComment)//注释类型结点,没有子结点
            {
                string comment="//"+curnode.Value.ToString();
                comment=comment.PadLeft (comment .Length +padding );
                this.listBox1.Items.Add(comment );
                if (curnode.NextSibling != null)//这里要注意,注释也存在兄弟结点
                {
                    showinlistbox(curnode.NextSibling,padding );
                }
            }
        }

——————————————————————————————————————————————

顺序遍历xml结点,基于流模式

    public partial class xmlReader : Form
    {
        public xmlReader()
        {
            InitializeComponent();
        }
        private XmlTextReader bookReader;//定义流模式的读对象
        private void btnGo_Click(object sender, EventArgs e)
        {
            string xmlfile = "BookStore.xml";
            this.bookReader = new XmlTextReader(xmlfile);//初始化读对象
            this.bookReader.WhitespaceHandling = WhitespaceHandling.None;//设置不读取Whitespace类节点
            btnNext.Enabled = true;
            txtNodeType.Text = "ready!";
            txtValue.Text = "ready";
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (bookReader.Read())//开始向前读,每单击一次,向前读一次
            {
                txtNodeType.Text = bookReader.NodeType.ToString();//节点类型
                txtValue.Text = bookReader.Value.ToString();//去节点的值
                txtName.Text = bookReader.Name.ToString();//读取节点名称
            }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            bookReader.Close();//关闭读对象
            this.Close();
        }
    }

上面是读,下面是写:

            private XmlTextWriter bookWriter= new XmlTextWriter("newBook.xml", Encoding.UTF8);
            this.bookWriter.Formatting = Formatting.Indented;
            this.bookWriter.Indentation = 2;
            this.bookWriter.WriteStartDocument();
            this.bookWriter.WriteComment("这是一本书");
            this.bookWriter.WriteStartElement("books");

            this.bookWriter.WriteStartElement("Book");
            this.bookWriter.WriteAttributeString("Title",this .txtBook .Text );

            this.bookWriter.WriteStartElement(this .txtAuthor .Text );//如果有子节点就不能用下面的string方法
            this.bookWriter.WriteElementString("姓名", this.txtName.Text);
            this.bookWriter.WriteElementString("单位", this.txtCompany.Text);
            this.bookWriter.WriteEndElement();

            this.bookWriter.WriteElementString("价格", this.txtPrice.Text);
            this.bookWriter.WriteElementString("时间", this.txtTime.Text);

上面写的xml文件:


XML节点类型:

XPath语言


posted on 2012-04-13 17:03    阅读(1156)  评论(0编辑  收藏  举报