在Mono中导航后,XPathNavigator.SchemaInfo为空
我正在将C#与Mono一起使用,我想使用XPathNavigator浏览一个经过XML模式验证的XML文档。当我遍历文档时,我可以通过XPathNavigator.SchemaInfo属性获得每个元素的XML模式信息。但是,在调用XPathNavigator.MoveToFirstChild()之后,XPathNavigator.SchemaInfo=null。这里有一个例子在Mono中导航后,XPathNavigator.SchemaInfo为空,mono,xpathnavigator,system.xml,Mono,Xpathnavigator,System.xml,我正在将C#与Mono一起使用,我想使用XPathNavigator浏览一个经过XML模式验证的XML文档。当我遍历文档时,我可以通过XPathNavigator.SchemaInfo属性获得每个元素的XML模式信息。但是,在调用XPathNavigator.MoveToFirstChild()之后,XPathNavigator.SchemaInfo=null。这里有一个例子 using System; using System.IO; using System.Xml; using Syst
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>
有人对发生了什么或我做错了什么有意见吗
谢谢
戴夫
PS我说“在mono中”,因为这是我正在使用的,我还不能在Windows上确认。此外,它的运行时版本是.NET4.0,发生在调试和发布中
更新我刚刚在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-
我通过从XmlSchemaSet中的根XmlSchemaElement进行遍历并通过XmlQualifiedName记录所有XmlSchemaElement和XmlSchemaAttribute来解决这个问题,然后在XPathNavigator遍历XmlDocument时,使用XPathNavigator中的XmlQualifiedName来查找XmlSchemaElement/XmlSchemaAttribute。它有点可行,但也不可行,因为XmlQualifiedName不足以唯一标识模式中的元素,例如元素可以在模式中的不同位置使用不同的maxOccurs/minOccurs值。Mono JIT编译器版本2.10.5(Debian 2.10.5-1ubuntu0.1)版权(C)2002-2011 Novell,Inc,Xamarin,公司和贡献者。www.mono-project.com TLS:u thread SIGSEGV:altstack通知:epoll体系结构:amd64已禁用:无其他:softdebug LLVM:受支持,未启用。GC:在Ubuntu11.10上包括Boehm(带有类型化GC和并行标记),如果它在Windows上工作(使用MS.NET,而不是Mono),但在Mono上不工作,那么:首先,将Mono更新到3.x,再次测试,如果它仍然不工作,那么在这里提交一个bug中的最小测试用例:
XPathNavigator.SchemaInfo is not null
Called XPathNavigator.MoveToRoot()
XPathNavigator.LocalName after .MoveToFirstChild() succeeded = TestElement
XPathNavigator.NodeType value = Element
XPathNavigator.SchemaInfo is null
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 . . .