1.使用XSD模式验证XML数据
验证XML文档的步骤
(1)定义一个ValidationEventHandle事件处理方法。
(2)创建XmlReaderSettings对象的实例。
(3)将ValidationEventHandle与XmlReaderSettings相关联
(4) 将XmlReaderSettings的ValidationType属性设置为ValidationType.Schema.
(5)通过XmlReaderSettings类的属性将XSD模式添加至XmlReaderSettings类
(6)在解析XML数据的时候,XMLReader类使用Read方法验证XML文档。
Xml
<?xml version="1.0" encoding="utf-8" ?>
<authors>
<author>
<au_id>123123</au_id>
<au_lname>White</au_lname>
<au_fname>Johnson</au_fname>
<phone>23446345</phone>
<address>10932 Bigge Rd.</address>
<city>Menlo Park</city>
<state>CA</state>
<zip>049366</zip>
<contract>true</contract>
</author>
<author>
<au_id>123123</au_id>
<au_lname>Green</au_lname>
<au_fname>Johnson</au_fname>
<phone>23446345</phone>
<address>10932 Bigge Rd.</address>
<city>Menlo Park</city>
<state>CA</state>
<zip>049366</zip>
<contract>true</contract>
</author>
</authors>
XSD
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="Authors"
targetNamespace="http://tempuri.org/Authors.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/Authors.xsd"
xmlns:mstns="http://tempuri.org/Authors.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="authors">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="au_id" type="xs:string"/>
<xs:element name="au_lname" type="xs:string"/>
<xs:element name="au_fname" type="xs:string"/>
<xs:element name="phone" type="xs:string"/>
<xs:element name="address" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="zip" type="xs:unsignedInt"/>
<xs:element name="contract" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
C#代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
private StringBuilder _builder = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
string xmlPath = Request.PhysicalApplicationPath +
@"\App_Data\Authors.xml";
string xsdPath = Request.PhysicalApplicationPath +
@"\App_Data\Authors.xsd";
XmlReader reader = null;
XmlReaderSettings setting = new XmlReaderSettings();
setting.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(setting_ValidationEventHandler);
setting.ValidationType = ValidationType.Schema;
setting.Schemas.Add(null,XmlReader.Create(xsdPath));
reader = XmlReader.Create(xmlPath,setting);
while (reader.Read())
{
}
if (_builder.ToString() == string.Empty)
{
Response.Write("Validation completed successfully.");
}
else
Response.Write("Validation Faild.<br>" + _builder.ToString());
}
void setting_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)
{
_builder.Append("Validation error :"+e.Message+"<br>");
Response.Write(_builder.ToString());
}
2. 模式缓存 C#代码不同
C#代码
private StringBuilder _builder = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
string xmlPath = Request.PhysicalApplicationPath +
@"\App_Data\Authors.xml";
string xsdPath = Request.PhysicalApplicationPath +
@"\App_Data\Authors.xsd";
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(null,xsdPath);
XmlReader reader = null;
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler);
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schemaSet;
reader = XmlReader.Create(xmlPath,settings);
while (reader.Read())
{
}
if (_builder.ToString() == string.Empty)
Response.Write("Validation completed successfully");
else
Response.Write("Validation faild.<Br>"+_builder.ToString());
}
void settings_ValidationEventHandler(object sender, ValidationEventArgs e)
{
_builder.Append("Validation error: "+e.Message+"<br>");
}