下面是一段XML schema文档
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com"
xmlns="http://www.w3school.com"
elementFormDefault="qualified">
<xsd:element name ="Book" type ="xsd:string"></xsd:element>
</xsd:schema>
此XML schema文档只声明了一个Book元素,保存为XMLSchema1.xsd文件下面是引用上面schema文档的XML文档
<?xml version="1.0" encoding="utf-8"?>
<Boo xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com XMLSchema1.xsd"> //引用XMLSchema1.xsd文件
刘德华
</Boo>
这段XML能在浏览器中正确显示
我的问题:
schema文档中不是声明了一个Book元素吗,XML文档是引用schema文档的,而XML文档中的元素是Boo,为什么不报错呢?还能在浏览器中正确显示。
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3school.com"
xmlns="http://www.w3school.com"
elementFormDefault="qualified">
<xsd:element name ="Book" type ="xsd:string"></xsd:element>
</xsd:schema>
此XML schema文档只声明了一个Book元素,保存为XMLSchema1.xsd文件下面是引用上面schema文档的XML文档
<?xml version="1.0" encoding="utf-8"?>
<Boo xmlns="http://www.w3schools.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3schools.com XMLSchema1.xsd"> //引用XMLSchema1.xsd文件
刘德华
</Boo>
这段XML能在浏览器中正确显示
我的问题:
schema文档中不是声明了一个Book元素吗,XML文档是引用schema文档的,而XML文档中的元素是Boo,为什么不报错呢?还能在浏览器中正确显示。
可能引用schema文档并不是要严格按它来吧,希望牛人解释下。
可能引用失败了。
其实W3C上面的例子很好实践,不用这样引用,一般都是XmlReaderSettings方法验证。下面是完整的C#例子,已验证
http://www.cnblogs.com/chenxizhang/archive/2009/06/19/1507121.html
w3school
<Boo xmlns="http://www.w3school.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3school.com XMLSchema1.xsd">
刘德华
</Boo>
{XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
XmlReader reader = XmlReader.Create(@"L:\mvc2\WindowsFormsApplication2\XMLFile1.xml", settings);
while (reader.Read()) ;}
private static void ValidationCallBack(object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Warning)
MessageBox.Show("警告:" + args.Message);
else
MessageBox.Show("错误:" + args.Message);}
http://w3schools.invisionzone.com/index.php?showtopic=34669