我正在使用c#和Mono,并且我想使用XPathNavigator导航XML Schema验证的XmlDocument。关键是,当我遍历文档时,我可以通过XPathNavigator获取每个元素的XML SChema信息。SchemaInfo财产。然而,在我调用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;
}
}
}
}
With this 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,发生在调试和发布。
UPDATE我刚刚在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 . . .
也许是单声道的东西?
我最终提交了一个bug - https://bugzilla.xamarin.com/show_bug.cgi?id=9541
我正在通过从XmlSchemaSet中的根XmlSchemaElement遍历并通过XmlQualifiedName记录所有XmlSchemaElements和XmlSchemaAttributes来解决问题,然后在通过XPathNavigator遍历XmlDocument时,使用XPathNavigator中的XmlQualifiedName查找XmlSchemaElement/XmlSchemaAttribute。它有点工作,但也没有,因为XmlQualifiedName不足以唯一地标识模式中的元素实例,例如,一个元素可以在模式中的不同位置使用不同的maxOccurs/minOccurs值。