下面是XSD:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:bookstore-schema" elementFormDefault="qualified" targetNamespace="urn:bookstore-schema">
<xsd:element name="bookstore" type="bookstoreType" />
<xsd:element name="comment" type="xsd:string" />
<xsd:element name="author" type="authorName"/>
<xsd:complexType name="authorName">
<xsd:sequence>
<xsd:element name="first-name" type="xsd:string" />
<xsd:element name="last-name" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookstoreType">
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="book" type="bookType" />
<xsd:element ref="comment" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="bookType">
<xsd:sequence>
<xsd:element name="title" type="xsd:string" />
<xsd:element ref="author" />
<xsd:element name="price" type="xsd:decimal" />
</xsd:sequence>
<xsd:attribute name="genre" type="xsd:string" />
</xsd:complexType></xsd:schema>
下面是代码:
using System;
using System.Xml;
using System.Xml.Schema;
namespace FragmentValidation {
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1 {
/// <summary>
/// The main entry point for the application.
/// </summary>
System.Boolean m_success; [STAThread]
static void Main(string[] args) {
//
// TODO: Add code to start application here.
//
XmlValidatingReader reader  = null;
XmlSchemaCollection myschema = new XmlSchemaCollection();
ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors );
try {
//Create the XML fragment to be parsed.
//String xmlFrag = "<author  xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
// "<first-name>Herman</first-name>" +
// "<last-name>Melville</last-name>" +
// "</author>"; String xmlFrag = "<first-name xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>Herman</first-name>"; //Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None); //Implement the reader.
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
//Add the schema.
myschema.Add("urn:bookstore-schema", "c:\\Books.xsd"); //Set the schema type and add the schema to the reader.
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add(myschema); while (reader.Read()) {
} Console.WriteLine("Completed validating xmlfragment");
}
catch (XmlException XmlExp) {
Console.WriteLine(XmlExp.Message);
}
catch(XmlSchemaException XmlSchExp) {
Console.WriteLine(XmlSchExp.Message);
}
catch(Exception GenExp) {
Console.WriteLine(GenExp.Message);
}
finally {
Console.Read();
} }
public static void ShowCompileErrors(object sender, ValidationEventArgs args) {
Console.WriteLine("Validation Error: {0}", args.Message);
} }
}
其实就是http://support.microsoft.com/default.aspx?scid=kb;en-us;318504#top上的代码,但是我改了一下要验证的元素,验证一个没有全局元素声明的element,但是报告未声明“urn:bookstore-schema:first-name”元素。 , (1, 2)处发生了错误。
看来XML片段的验证只支持全局元素生命.
虽然也可以将局部声明转换成全局声明,但是对于复杂的XSD来说,很复杂,而且不一定等价.
请问有什么好办法解决这个问题,谢谢