xsd验证xml - 含源码
关键代码:
class XmlSchemaSetExample { private string logpath; public void handle(String names, String xml, String xsd, String lpath) { this.logpath = lpath; XmlReaderSettings booksSettings = new XmlReaderSettings(); booksSettings.Schemas.Add(names, xsd); booksSettings.ValidationType = ValidationType.Schema; booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler); XmlReader books = XmlReader.Create(xml, booksSettings); while (books.Read()) { } } private void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e) { LogManager.LogFielPrefix = "EADI "; LogManager.LogPath = @logpath; if (e.Severity == XmlSeverityType.Warning) { LogManager.WriteLog(LogFile.Warning, "WARNING: " + e.Message); // MessageBox.Show("WARNING: " + e.Message); // Console.Write("WARNING: "); //Console.WriteLine(e.Message); } else if (e.Severity == XmlSeverityType.Error) { LogManager.WriteLog(LogFile.Error, "ERROR: " + e.Message); // MessageBox.Show("ERROR: " + e.Message); //Console.Write("ERROR: "); //Console.WriteLine(e.Message); } } }
调用方法:
XmlSchemaSetExample xmlSchemaSetExample = new XmlSchemaSetExample(); xmlSchemaSetExample.handle(textNameSpace.Text, path.ToString() + ".xml", path.ToString() + ".xsd", textlog.Text);
测试的XML和XSD(文件名字一定要相同)
contosoBooks.xml
<bookstore xmlns="http://www.contoso.com/books"> <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0"> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjamin</first-name> <last-name>Franklin</last-name> </author> <price>8.99d</price> </book> <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2"> <title>The Confidence Man</title> <author> <first-name>Herman</first-name> <last-name>Melville</last-name> </author> <price>11.99d</price> </book> <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6"> <title>The Gorgias</title> <author> <name>Plato</name> </author> <price>9.99</price> </book> </bookstore>
contosoBooks.xsd
<?xml version="1.0" encoding="utf-8"?> <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="bookstore"> <xs:complexType> <xs:sequence> <xs:element maxOccurs="unbounded" name="book"> <xs:complexType> <xs:sequence> <xs:element name="title" type="xs:string" /> <xs:element name="author"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="name" type="xs:string" /> <xs:element minOccurs="0" name="first-name" type="xs:string" /> <xs:element minOccurs="0" name="last-name" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="price" type="xs:decimal" /> </xs:sequence> <xs:attribute name="genre" type="xs:string" use="required" /> <xs:attribute name="publicationdate" type="xs:date" use="required" /> <xs:attribute name="ISBN" type="xs:string" use="required" /> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
详情请看附件...