生命之价
---程序员

 在这儿我找了一个例子来说明一下吧!希望大家有什么好的代码一起分享,一起学习!共同进步!

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

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

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

因为以前没看过xml,现在是刚开始学习,所以耽搁了几天。这是从同事那儿来的代码,也没做修改就贴出来了!
Authors.xml
下边是Schema:
Authors.xsd
posted on 2007-04-12 20:23  Freeman Shen  阅读(259)  评论(0编辑  收藏  举报