使用XmlSchemaSet验证

XmlSchemaSet包含 XML 架构定义语言 (XSD) 架构的缓存

不使用XmlSchemaSet和使用XmlSchemaSet的比较

1.没使用XmlSchemaSet
 1 <% @ Page Language = " C# " %>
 2 <% @ Import Namespace = " System.Xml "   %>
 3 <% @ Import Namespace = " System.Xml.Schema "   %>
 4
 5 < script runat = " server " >     
 6      private  StringBuilder _builder  =   new  StringBuilder();
 7      void  Page_Load( object  sender, EventArgs e)
 8      {
 9        string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/Authors.xml";    
10        string xsdPath = Request.PhysicalApplicationPath + @"/App_Data/Authors.xsd";
11        XmlReader reader = null;        
12        XmlReaderSettings settings = new XmlReaderSettings();
13        settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventHandler);
14        settings.ValidationType = ValidationType.Schema;        
15        settings.Schemas.Add(null, XmlReader.Create(xsdPath));
16        reader = XmlReader.Create(xmlPath, settings);
17        while (reader.Read()) 
18        {            
19        }

20        if (_builder.ToString() == String.Empty)
21            Response.Write("Validation completed successfully.");
22        else
23            Response.Write("Validation Failed. <br>" + _builder.ToString());
24    }

25
26      // 报错
27      void  ValidationEventHandler( object  sender, ValidationEventArgs args)
28      {        
29        _builder.Append("Validation error: " + args.Message + "<br>");                
30    }
    
31   
32 </ script >
33 < html xmlns = " http://www.w3.org/1999/xhtml "   >
34 < head runat = " server " >
35      < title > XSD Validation </ title >
36 </ head >
37 < body >
38      < form id = " form1 "  runat = " server " >
39      < div >                 
40      </ div >
41      </ form >
42 </ body >
43 </ html >
44


执行页面以后,无法修改XSD文件,会出现提示,文件正在使用




 1 void  Page_Load( object  sender, EventArgs e)
 2      {        
 3        string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/Authors.xml";    
 4        string xsdPath = Request.PhysicalApplicationPath + @"/App_Data/Authors.xsd";
 5        XmlSchemaSet schemaSet = new XmlSchemaSet();
 6        schemaSet.Add(null, xsdPath);
 7        XmlReader reader = null;        
 8        XmlReaderSettings settings = new XmlReaderSettings();
 9        settings.ValidationEventHandler += new ValidationEventHandler(this.ValidationEventHandler);
10        settings.ValidationType = ValidationType.Schema;        
11        settings.Schemas = schemaSet;        
12        reader = XmlReader.Create(xmlPath, settings);


使用XmlSchemaSet后就不会出现这样的错误了

记录一下
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值