MSDN的一个例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
namespace ValidateXmlByXsd
{
class Program
{
static void Main(string[] args)
{
string xmlpath = @"../../Instances/AuthorError.xml";
string xsdpath = @"../../Instances/Authors.xsd";
XmlReader reader = null;
XmlReaderSettings setting = new XmlReaderSettings();
setting.ValidationEventHandler+=new ValidationEventHandler(setting_ValidationEventHandler);
setting.ValidationType = ValidationType.Schema;
setting.Schemas.Add("", XmlReader.Create(xsdpath));
reader = XmlReader.Create(xmlpath,setting);
while (reader.Read())
{ }
Console.Read();
}
public static void setting_ValidationEventHandler(object sender,ValidationEventArgs e)
{
Console.WriteLine("validation error:" + e.Message);
}
}
}
Xml Demo:
<?xml version="1.0" encoding="utf-8" ?>
<authors>
<author>
<au_id>172-32-1176</au_id>
<au_lname>White</au_lname>
<au_fname>Johnson</au_fname>
<phone>408 496-7223</phone>
<address>10932 Bigge Rd.</address>
<city>Menlo Park</city>
<state>CA</state>
<zip>94025</zip>
<contract>true</contract>
</author>
<author>
<au_id>213-46-8915</au_id>
<au_lname>Green</au_lname>
<au_fname>Marjorie</au_fname>
<phone>415 986-7020</phone>
<address>309 63rd St. #411</address>
<city>Oakland</city>
<state>CA</state>
<zip>94618</zip>
<contract>true</contract>
</author>
</authors>
Schema DEMO:
<?xml version="1.0" encoding="utf-16"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" 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:boolean" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>