0
我有一个验证针对XSD文档的XML文档,如下C#脚本:对XSD XML验证总是返回true
static bool IsValidXml(string xmlFilePath, string xsdFilePath)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, xsdFilePath);
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Compile();
try
{
XmlReader xmlRead = XmlReader.Create(xmlFilePath, settings);
while (xmlRead.Read())
{ };
xmlRead.Close();
}
catch (Exception e)
{
return false;
}
return true;
}
我一直在寻找一些MSDN文章和问题之后,这个编译在这里,这是解决方案。它确实正确地验证了XSD形成良好(如果我弄乱了文件,则返回false),并检查XML是否形成良好(当与之混淆时也返回false)。
我也试过以下,但它完全一样的事情:
static bool IsValidXml(string xmlFilePath, string xsdFilePath)
{
XDocument xdoc = XDocument.Load(xmlFilePath);
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add(null, xsdFilePath);
try
{
xdoc.Validate(schemas, null);
}
catch (XmlSchemaValidationException e)
{
return false;
}
return true;
}
我甚至拉到一个完全随机的XSD从互联网和它扔进两个脚本,它仍然验证双方。我在这里错过了什么?
在SSIS作业中使用.NET 3.5。
你还没有提供任何细节,但是如果你使用随机模式验证XML,那么这可能是预期的。如果文档在模式中没有任何匹配的元素,您将得到最好的警告。 –
[使用XSD正确验证XML文档]可能的副本(http://stackoverflow.com/questions/16755058/validating-xml-documents-with-xsd-correctly) –
检查XML文档中的名称空间是否与正在由模式定位。可能有助于发布您试图验证的模式和xml文件的示例 – SCB