【XML XSD】XSD 架构 验证 XML

 

 

 

booksSchemaFail.xml

xmlns 声明了xml 所引用的 schema的name,不是文件名,是xsd 里定义的 targetNamespace 名

 

 

<?xml version='1.0'?>

<bookstore xmlns="urn:bookstore-schema">

  <book>

    <author>

      <first-name>Benjamin</first-name>

      <last-name>Franklin</last-name>

    </author>

  </book>

  <book genre="novel">

    <title>The Confidence Man</title>

    <author>

      <first-name>Herman</first-name>

      <last-name>Melville</last-name>

    </author>

    <price>11.99</price>

  </book>

  <book genre="philosophy">

    <title>The Gorgias</title>

    <author>

      <name>Plato</name>

    </author>

    <price>9.99</price>

  </book>

</bookstore>

 

 

 

 

book.xsd

 

定义都以  <xsd:  开始; 首先是<xsd:schema 元素是 架构文档中第一的顶级元素 

 xmlns是XML Namespaces的缩写,中文名称是XML命名空间。

URI http://www.w3.org/XML/1998/namespace 用于 XML 命名空间声明。此 URI 是保留的命名空间,不能包括在 XML 命名空间声明中。

 

 

xsd:element 定义名称,type有自带类型和复杂类型.

xsd:complexType  定义复杂类型

xsd:sequence  定义元素出现的顺序

maxOccurs 定义的是最大出现次数,unbounded 就是可以多次

 

xmlns:xsd  默认命名空间 xsd 为别名

xmlns  默认明明空间名称

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

    xmlns="urn:bookstore-schema"

    elementFormDefault="qualified"

    targetNamespace="urn:bookstore-schema">

 <xsd:element name="bookstore" type="bookstoreType"/>

 <xsd:complexType name="bookstoreType">

  <xsd:sequence maxOccurs="unbounded">

   <xsd:element name="book"  type="bookType"/>

  </xsd:sequence>

 </xsd:complexType>

 <xsd:complexType name="bookType">

  <xsd:sequence>

   <xsd:element name="title" type="xsd:string"/>

   <xsd:element name="author" type="authorName"/>

   <xsd:element name="price"  type="xsd:decimal"/>

  </xsd:sequence>

  <xsd:attribute name="genre" type="xsd:string"/>

 </xsd:complexType>

 <xsd:complexType name="authorName">

  <xsd:sequence>

   <xsd:element name="first-name"  type="xsd:string"/>

   <xsd:element name="last-name" type="xsd:string"/>

  </xsd:sequence>

 </xsd:complexType>

</xsd:schema>

 

 

 

在代码中用xsd来验证xml

 

 

步骤:1.设置xsd的缓存, XmlSchemaSet

        2.  XmlReaderSettings设置指定验证type,验证的具体方法(及验证时候遇到错误打印出来的方法)

        3. XMLreader,设置xmlreader  的creater方法

        4.遍历读取,直到reader为空

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml;

using System.Xml.Schema;

namespace XML学习_控制台应用程序

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                #region XMLDocument操作xml

                /*

                XmlDocument a = new XmlDocument();

                a.LoadXml("<book isbn=\"900-100-SSS333333333\"><name>名字</name></book>");

                XmlNode root = a.DocumentElement;  //获取文档根节点

                root.InnerText = "wo lai le";

                XmlElement xchild = a.CreateElement("price");

                xchild.SetAttribute("yuanjia", "200");

                xchild.SetAttribute("xianjia","19");

                xchild.InnerText = "19.00";

                root.InsertBefore(xchild, root.FirstChild);

               

                //root.AppendChild(xchild);

                //root.RemoveChild(root.ChildNodes[0]);

                //root.RemoveChild(xchild);

                a.Save("data.xml");

                */

                #endregion

                #region xsd验证xml

                XmlSchemaSet sc = new XmlSchemaSet();

                sc.Add("urn:bookstore-schema", "books.xsd");   // 命名空间,xsd名字

                XmlReaderSettings settings = new XmlReaderSettings();

                settings.ValidationType = ValidationType.Schema;

                settings.Schemas = sc;

                settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);

                XmlReader reader = XmlReader.Create("booksSchemaFail.xml",settings);

                while (reader.Read()) ;

                #endregion

            }

            catch (Exception eee)

            {

                Console.WriteLine(eee.Message);

            }

            finally

            {

                Console.ReadLine();

            }

        }

        // Display any validation errors.

        private static void ValidationCallBack(object sender, ValidationEventArgs e)

        {

            Console.WriteLine("Validation Error: {0}", e.Message);

        }

    }

}

你可能感兴趣的:(xml)