| Schemas | 获取或设置在执行架构验证时使用的 XmlSchemaSet。 |
| ValidationType | 获取或设置一个值,该值指示 XmlReader 在读取时是否执行验证或类型分配。 |

book.xml
book.xsd
Default.aspx
Default.aspx.cs
using ...System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
public partial class _Default : System.Web.UI.Page 
...{
protected void Page_Load(object sender, EventArgs e) 
...{
XmlReaderSettings settings = new XmlReaderSettings();
//验证类型设置为模式(xsd)
settings.ValidationType = ValidationType.Schema;
//为XmlReaderSettings对象添加模式
//第一个参数是targetNamespace的值,null表示采用XSD文件里targetNamespace属性的值
//如果要明确传递此参数,务必与targetNamespace的值一致
//第二个参数一定要采用绝对路径或物理路径,不能采用相对路径
settings.Schemas.Add(null, Server.MapPath("book.xsd"));
//添加验证错误的处理事件
settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(settings_ValidationEventHandler);
//同理第一个参数必须是绝对路径或物理路径
XmlReader reader = XmlReader.Create(Server.MapPath("book.xml"), settings);
while (reader.Read()) 
...{
}
reader.Close();
this.ltlMsg.Text += "End";
}
void settings_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e) 
...{
this.ltlMsg.Text += e.Message + "<br />";
}
}