.net中使用XML Schema验证

  相关的.net类
XmlReader
XmlReaderSettings
XmlSchemaSet

在.net里面验证Schema是件很容易的事 只要在XmlReader打开文件的时候填上一个 要求验证的setting就行了 建议不要在这个基础上乱封装一些XmlSchemaValidator之类的东西。

 

例子 在某个asp.net page 里面使用schema 

(语言C#  版本要求.net3.0+)

using System;
using System.Xml;
using System.Xml.Schema;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
 
        XmlSchemaSet sc = new XmlSchemaSet();
        // Add the schema to the collection.
        sc.Add(null, @"C:/Documents and Settings/csf/My Documents/Visual Studio 2008/WebSite2/xhtml1-transitional.xsd");
 
 
        // Set the validation settings.
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.ValidationType = ValidationType.Schema;
        settings.Schemas = sc;
        settings.ProhibitDtd = false;
        settings.ValidationEventHandler += new ValidationEventHandler(delegate(object senderc, ValidationEventArgs ec) { Response.Write(ec.Message); });
 
        // Create the XmlReader object.
        XmlReader reader = XmlReader.Create(@"C:/Documents and Settings/csf/My Documents/Visual Studio 2008/WebSite2/HTMLPage2.htm", settings);
 
        // Parse the file.
        while (reader.Read()) ;
        reader.Close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值