我将 C# 与 Mono 一起使用,并且我想使用 XPathNavigator 导航经过 XML Schema 验证的 XmlDocument。关键是当我遍历文档时,我可以通过 XPathNavigator.SchemaInfo 属性获取每个元素的 XML Schema 信息。但是,在我调用 XPathNavigator.MoveToFirstChild() 之后,XPathNavigator.SchemaInfo = null。这是一个例子
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Schema;
namespace XmlSchemaTest
{
class Program
{
static void Main(string[] args)
{
XmlDocument xmlDocument = ReadAndValidateXmlFile(@"../test.xml", @"../test.xsd");
if (xmlDocument == null)
Console.WriteLine("Cannot open document or it didn't validate.");
else
{
XPathNavigator xpathNavigator = xmlDocument.CreateNavigator();
Console.WriteLine("XPathNavigator.SchemaInfo is " + ((xpathNavigator.SchemaInfo == null) ? "null" : "not null"));
xpathNavigator.MoveToRoot();
Console.WriteLine("Called XPathNavigator.MoveToRoot()");
if(xpathNavigator.MoveToFirstChild())
{
Console.WriteLine("XPathNavigator.LocalName after .MoveToFirstChild() succeeded = " + xpathNavigator.LocalName);
Console.WriteLine("XPathNavigator.NodeType value = " + xpathNavigator.NodeType.ToString());
Console.WriteLine("XPathNavigator.SchemaInfo is " + ((xpathNavigator.SchemaInfo == null) ? "null" : "not null"));
}
}
//Console.ReadLine();
}
private static XmlDocument ReadAndValidateXmlFile(string xmlPath, string xsdPath)
{
// Load the XML Schema
bool anyValidationErrors = false;
XmlSchemaSet oSchemaSet = new XmlSchemaSet();
ValidationEventHandler Handler = new ValidationEventHandler((object sender, ValidationEventArgs args) => {Console.WriteLine(args.Message); anyValidationErrors = true;} );
oSchemaSet.ValidationEventHandler += Handler;
XmlSchema oSchema = null;
using (StreamReader sr = new StreamReader(xsdPath)) {
oSchema = XmlSchema.Read(sr, Handler);
}
if (anyValidationErrors || (oSchema == null)) {
Console.WriteLine("Schema validation errors");
return null;
}
oSchemaSet.Add(oSchema);
// Set up the Xml reader to do schema validation
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.ValidationType = ValidationType.Schema;
xmlReaderSettings.Schemas.Add(oSchemaSet);
anyValidationErrors = false;
xmlReaderSettings.ValidationEventHandler += new ValidationEventHandler((object sender, ValidationEventArgs args) => {Console.WriteLine(args.Message); anyValidationErrors = true;} );
// Load the Xml and validate against schemer
using (XmlReader xmlReader = XmlReader.Create(xmlPath, xmlReaderSettings))
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(xmlReader);
if (anyValidationErrors) {
Console.WriteLine("Xml validation errors");
return null;
}
else
return xmlDocument;
}
}
}
}
有了这个 XSD
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TEST"
targetNamespace="urn:test"
xmlns:tst="urn:test"
xmlns="urn:test"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="TestElement">
<xs:complexType>
<xs:sequence>
<xs:element name="SubEle">
<xs:complexType>
<xs:sequence>
<xs:element name="ChildEle" type="xs:unsignedInt" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
而这个 XML
<?xml version="1.0" encoding="utf-8" ?>
<tst:TestElement xmlns:tst="urn:test">
<tst:SubEle>
<tst:ChildEle>123</tst:ChildEle>
</tst:SubEle>
</tst:TestElement>
给出这个输出
XPathNavigator.SchemaInfo is not null
Called XPathNavigator.MoveToRoot()
XPathNavigator.LocalName after .MoveToFirstChild() succeeded = TestElement
XPathNavigator.NodeType value = Element
XPathNavigator.SchemaInfo is null
有人对正在发生的事情或我做错了什么有建议吗?
谢谢戴夫
PS我说“单声道”是因为那是我正在使用的,我还不能在 Windows 上确认。此外,它的运行时版本是 .Net 4.0,发生在 Debug 和 Release 中。
更新我刚刚在 Windows 上尝试过这个并得到了这个结果
XPathNavigator.SchemaInfo is not null
Called XPathNavigator.MoveToRoot()
XPathNavigator.LocalName after .MoveToFirstChild() succeeded = TestElement
XPathNavigator.NodeType value = Element
XPathNavigator.SchemaInfo is not null
Press any key to continue . . .
所以也许是 Mono 的事情?