xsd(一)

来源:互联网 发布:针锋对决网络剧百度云 编辑:程序博客网 时间:2024/04/29 20:41

books.xml:

<?xml version="1.0" encoding="utf-8"?>
<!--<?xml-stylesheet type="text/xsl" href="books.xslt"?>-->
<library xmlns:l="http://tempuri.org/books.xsd">
  <name>首都图书馆</name>
  <address>朝阳区华威桥南</address>
  <books>
    <book type="math">
      <id>0000</id>
    </book>
    <book type="computer">
      <id>0001</id>
      <name>Xml初学</name>
      <publisher>人民出版社</publisher>
      <fee>100.54</fee>
    </book>
    <book type="computer">
      <id>0002</id>
      <name>XSD定义</name>
      <author>子弟</author>
      <publisher>子弟出版社</publisher>
      <publishdate>2010-05-0-18</publishdate>
      <fee>102.54</fee>
    </book>
  </books>
</library>

 

 

books.xsd:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="books"
 attributeFormDefault="unqualified"
    elementFormDefault="qualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="library">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="name" type="xs:string" />
    <xs:element name="address" type="xs:string" />
    <xs:element name="books">
     <xs:complexType>
      <xs:sequence>
       <xs:element maxOccurs="unbounded" name="book">
        <xs:complexType>
         <xs:sequence>
          <xs:element name="id" type="xs:unsignedByte" />
          <xs:element minOccurs="0" name="name" type="xs:string" />
          <xs:element minOccurs="0" name="author" type="xs:string" />
          <xs:element minOccurs="0" name="publisher" type="xs:string" />
          <xs:element minOccurs="0" name="publishdate" type="xs:string" />
          <xs:element minOccurs="0" name="fee" type="xs:decimal" />
         </xs:sequence>
         <xs:attribute name="type" type="xs:string" use="required" />
        </xs:complexType>
       </xs:element>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
  </xs:complexType>
 </xs:element>
</xs:schema>

 

cs:

protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(Server.MapPath("htmlxml/books.xml"));
                XmlReader xsdReader = XmlReader.Create(Server.MapPath("htmlxml/books.xsd"));
                XmlReaderSettings settings = new XmlReaderSettings();
                //settings.Schemas.Add("http://tempuri.org/books.xsd", Server.MapPath("htmlxml/books.xsd"));
                settings.Schemas.Add(null, xsdReader);
                settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventHandler);
                settings.ValidationFlags = settings.ValidationFlags | XmlSchemaValidationFlags.ReportValidationWarnings;
                settings.ValidationType = ValidationType.Schema;

                XmlNodeReader xmlNodeReader = new XmlNodeReader(xmlDocument);
                XmlReader reader = XmlReader.Create(xmlNodeReader, settings);

                XmlDocument document = new XmlDocument();
                document.Load(reader);
                document.Validate(ValidationEventHandler);
                Response.Write("XSD验证成功/n");
            }
            catch (XmlException ex)
            {
                Response.Write("XmlException<br/>" + ex);
            }
            catch (XmlSchemaValidationException ex)
            {
                Response.Write("XmlSchemaValidationException<br/>" + ex);
            }
            catch (Exception ex)
            {
                Response.Write("Exception<br/>" + ex);
            }
        }
        public void ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs args)
        {
            if (args.Severity == XmlSeverityType.Warning)
                Console.Write("/nWARNING: ");
            else if (args.Severity == XmlSeverityType.Error)
                Console.Write("/nERROR: ");

            Console.WriteLine(args.Message);
            throw args.Exception;
        }

结果:

XSD验证成功

原创粉丝点击