代码改变世界

使用XmlSchemaSet验证

2006-04-16 15:18  Clingingboy  阅读(1080)  评论(0编辑  收藏  举报
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文件,会出现提示,文件正在使用




 1void 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后就不会出现这样的错误了

记录一下