现有Books.xml文件:
和books.xsd的样式文件
对其进行样式校验的Code如下:
<?
xml version="1.0" encoding="UTF-8"
?>
<!-- edited with XMLSpy v2006 sp2 U (http://www.altova.com) by 10030452 (Kodak) -->
< books xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation ="./books.xsd" >
< book >
< name > hi </ name >
< isbn > 1121 </ isbn >
</ book >
< book >
< name > hello </ name >
< isbn > 1122 </ isbn >
</ book >
< book >
< name > world </ name >
< isbn > 2211 </ isbn >
</ book >
< book >
< name > world1 </ name >
< isbn > 2212 </ isbn >
</ book >
</ books >
<!-- edited with XMLSpy v2006 sp2 U (http://www.altova.com) by 10030452 (Kodak) -->
< books xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation ="./books.xsd" >
< book >
< name > hi </ name >
< isbn > 1121 </ isbn >
</ book >
< book >
< name > hello </ name >
< isbn > 1122 </ isbn >
</ book >
< book >
< name > world </ name >
< isbn > 2211 </ isbn >
</ book >
< book >
< name > world1 </ name >
< isbn > 2212 </ isbn >
</ book >
</ books >
<?
xml version="1.0" encoding="UTF-8" standalone="yes"
?>
<!-- edited with XMLSpy v2006 sp2 U (http://www.altova.com) by 10030452 (Kodak) -->
<!-- W3C Schema generated by XMLSpy v2006 sp2 U (http://www.altova.com) -->
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >
< xs:element name ="book" >
< xs:complexType >
< xs:sequence >
< xs:element ref ="name" />
< xs:element ref ="isbn" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
< xs:element name ="books" >
< xs:complexType >
< xs:sequence >
< xs:element ref ="book" maxOccurs ="unbounded" />
</ xs:sequence >
</ xs:complexType >
< xs:unique name ="unique_bookname" >
< xs:selector xpath ="book" />
< xs:field xpath ="name" />
</ xs:unique >
</ xs:element >
< xs:element name ="isbn" >
< xs:simpleType >
< xs:restriction base ="xs:short" />
</ xs:simpleType >
</ xs:element >
< xs:element name ="name" >
< xs:simpleType >
< xs:restriction base ="xs:string" />
</ xs:simpleType >
</ xs:element >
</ xs:schema >
<!-- edited with XMLSpy v2006 sp2 U (http://www.altova.com) by 10030452 (Kodak) -->
<!-- W3C Schema generated by XMLSpy v2006 sp2 U (http://www.altova.com) -->
< xs:schema xmlns:xs ="http://www.w3.org/2001/XMLSchema" elementFormDefault ="qualified" >
< xs:element name ="book" >
< xs:complexType >
< xs:sequence >
< xs:element ref ="name" />
< xs:element ref ="isbn" />
</ xs:sequence >
</ xs:complexType >
</ xs:element >
< xs:element name ="books" >
< xs:complexType >
< xs:sequence >
< xs:element ref ="book" maxOccurs ="unbounded" />
</ xs:sequence >
</ xs:complexType >
< xs:unique name ="unique_bookname" >
< xs:selector xpath ="book" />
< xs:field xpath ="name" />
</ xs:unique >
</ xs:element >
< xs:element name ="isbn" >
< xs:simpleType >
< xs:restriction base ="xs:short" />
</ xs:simpleType >
</ xs:element >
< xs:element name ="name" >
< xs:simpleType >
< xs:restriction base ="xs:string" />
</ xs:simpleType >
</ xs:element >
</ xs:schema >
对其进行样式校验的Code如下:
private void button1_Click( object sender, EventArgs e)
{
try
{
using (XmlTextReader txtreader = new XmlTextReader( " //books.xml " ))
{
using (XmlValidatingReader oreader = new XmlValidatingReader(txtreader))
{
oreader.Schemas.Add( null , " //books.xsd " );
while (oreader.Read())
{
}
}
}
MessageBox.Show( " Validation successfully " );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click( object sender, EventArgs e)
{
try
{
using (XmlTextReader txtreader = new XmlTextReader( " //books.xml " ))
{
using (XmlValidatingReader oreader = new XmlValidatingReader(txtreader))
{
oreader.Schemas.Add( null , " //books.xsd " );
while (oreader.Read())
{
}
}
}
MessageBox.Show( " Validation successfully " );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}