1
我試圖使用XPathNavigator.CheckValidity
驗證XML文檔。不知何故,我能夠編寫通過此方法傳遞的測試,但現在(神祕地)不再傳遞。我能想到的唯一變化就是從.NET 2轉移到.NET 3.5,但在轉換過程中我找不到任何有關更改的文檔。XPathNavigator.CheckValidity驗證無效的XML文檔
下面是一個例子程序:
void Main()
{
try
{
GetManifest().CreateNavigator().CheckValidity(GetSchemaSet(), (sender, args) => {
// never get in here when debugging
if (args.Severity == XmlSeverityType.Error) {
throw new XmlSchemaValidationException("Manifest failed validation", args.Exception);
}
}); // returns true when debugging
}
catch (XmlSchemaValidationException)
{
// never get here
throw;
}
// code here runs
}
IXPathNavigable GetManifest()
{
using (TextReader manifestReader = new StringReader("<?xml version='1.0' encoding='utf-8' ?><BadManifest><bad>b</bad></BadManifest>"))
{
return new XPathDocument(manifestReader);
}
}
XmlSchemaSet GetSchemaSet()
{
var schemaSet = new XmlSchemaSet();
using (var schemaReader = new StringReader(Schema)){
schemaSet.Add(XmlSchema.Read(schemaReader, null));
}
return schemaSet;
}
const string Schema = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<xs:schema attributeFormDefault=""unqualified"" elementFormDefault=""qualified"" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""http://www.engagesoftware.com/Schemas/EngageManifest"">
<xs:element name=""EngageManifest"">
<xs:complexType>
<xs:all>
<xs:element name=""Title"" type=""xs:string"" />
<xs:element name=""Description"" type=""xs:string"" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>";
我試過在Validate XML with a XSD Schema without changing the XML using C#的解決方案,但我得到了同樣的結果。我必須失去了在這個驗證的事情是如何工作的一些大的方面考慮,但我看不到它...
那麼,有沒有一種方法可以在不需要文檔中的xmlns的情況下對模式進行驗證?如果沒有帶有指定targetNamespace的XSD的xmlns,是不可能驗證文檔的? – bdukes 2010-12-06 18:38:11