通过ValidationEventHandler验证XML的合法性

MSDN的一个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;

namespace ValidateXmlByXsd
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlpath = @"../../Instances/AuthorError.xml";
            string xsdpath = @"../../Instances/Authors.xsd";

            XmlReader reader = null;
            XmlReaderSettings setting = new XmlReaderSettings();
            setting.ValidationEventHandler+=new ValidationEventHandler(setting_ValidationEventHandler);

            setting.ValidationType = ValidationType.Schema;
            setting.Schemas.Add("", XmlReader.Create(xsdpath));
            reader = XmlReader.Create(xmlpath,setting);

            while (reader.Read())
            { }

            Console.Read();
        }

        public static void setting_ValidationEventHandler(object sender,ValidationEventArgs e)
        {
            Console.WriteLine("validation error:" + e.Message);
        }
    }
}

Xml Demo:

<?xml version="1.0" encoding="utf-8" ?>
<authors>
  <author>
    <au_id>172-32-1176</au_id>
    <au_lname>White</au_lname>
    <au_fname>Johnson</au_fname>
    <phone>408 496-7223</phone>
    <address>10932 Bigge Rd.</address>
    <city>Menlo Park</city>
    <state>CA</state>
    <zip>94025</zip>
    <contract>true</contract>
  </author>
  <author>
    <au_id>213-46-8915</au_id>
    <au_lname>Green</au_lname>
    <au_fname>Marjorie</au_fname>
    <phone>415 986-7020</phone>
    <address>309 63rd St. #411</address>
    <city>Oakland</city>
    <state>CA</state>
    <zip>94618</zip>
    <contract>true</contract>
  </author>
</authors>

 

Schema DEMO:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="authors">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="author">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="au_id" type="xs:string" />
              <xs:element name="au_lname" type="xs:string" />
              <xs:element name="au_fname" type="xs:string" />
              <xs:element name="phone" type="xs:string" />
              <xs:element name="address" type="xs:string" />
              <xs:element name="city" type="xs:string" />
              <xs:element name="state" type="xs:string" />
              <xs:element name="zip" type="xs:unsignedInt" />
              <xs:element name="contract" type="xs:boolean" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值