背景:
我们正在构建一个应用程序,允许我们的客户以预定义(即我们不控制)的 XML 格式提供数据。 XSD 由第三方提供给我们,我们希望在处理之前收到通过模式验证的 XML 文件。
问题:
我们提供的 XSD 包含默认和目标命名空间,这意味着如果客户提供不包含命名空间的 XML 文件,则验证将通过。我们显然不希望他们提供说他们通过但不应该通过的东西,但更大的问题是如果我找不到解决方案,我们将需要对每个元素进行大量额外检查XML 验证。
问题:
是否可以强制 .NET 执行验证并忽略提供的 XML 和 XSD 上的命名空间。即以某种方式“假设”命名空间已附加。
- 是否可以轻松、可靠地删除内存中的命名空间?
- 在这些情况下的最佳做法是什么?
我目前的解决方案:
- 每次更新时从 XSD 中删除命名空间(不应该经常更新。 这并不能回避这样一个事实,即如果他们提供命名空间,它仍将通过验证。
- 从 XSD 中删除命名空间,并找到一种方法每次都从传入的 XML 中删除命名空间。这似乎有很多代码来执行一些简单的事情。
- 在验证 XML 文件之前对其进行一些预验证,以确保它具有正确的命名空间。如果文件内容正确,则由于命名空间无效而使它们失败似乎是错误的。
- 创建一个没有命名空间的重复 XSD,但是如果他们只是提供了错误的命名空间或不同的命名空间,那么它仍然会通过。
示例 Xml:
<?xml version="1.0"?>
<xsd:schema version='3.09' elementFormDefault='qualified' attributeFormDefault='unqualified' id='blah' targetNamespace='urn:schemas-blah.com:blahExample' xmlns='urn:blah:blahExample' xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
...
</xsd:schema>
命名空间不同
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="urn:myCompany.com:blahExample1" attr1="2001-03-03" attr2="google" >
...
</root>
根本没有命名空间。
<?xml version="1.0" encoding="UTF-8" ?>
<root attr1="2001-03-03" attr2="google" >
...
</root>
最佳答案
试图解决同样的问题。我想出了一个我认为相当干净的解决方案。为清楚起见,我省略了对输入参数的一些验证。
首先,场景:有一个网络服务接收一个文件,该文件应该是“格式正确”的 xml 并且对 XSD 有效。当然,我们不相信“良好的形式”,也不相信“我们知道”是正确的对 XSD 有效。
这种 webservice 方法的代码如下所示,我认为它是不言自明的。
主要的兴趣点是验证发生的顺序,您不在加载前检查命名空间,而是在加载后检查,但是很干净。
我决定我可以接受一些异常处理,因为大多数文件都应该是“好的”,而且这是处理框架的方式(所以我不会反对)。
private DataTable xmlErrors;
[WebMethod]
public string Upload(byte[] f, string fileName) {
string ret = "This will have the response";
// this is the namespace that we want to use
string xmlNs = "http://mydomain.com/ns/upload.xsd";
// you could put a public url of xsd instead of a local file
string xsdFileName = Server.MapPath("~") + "//" +"shiporder.xsd";
// a simple table to store the eventual errors
// (more advanced ways possibly exist)
xmlErrors = new DataTable("XmlErrors");
xmlErrors.Columns.Add("Type");
xmlErrors.Columns.Add("Message");
try {
XmlDocument doc = new XmlDocument(); // create a document
// bind the document, namespace and xsd
doc.Schemas.Add(xmlNs, xsdFileName);
// if we wanted to validate if the XSD has itself XML errors
// doc.Schemas.ValidationEventHandler +=
// new ValidationEventHandler(Schemas_ValidationEventHandler);
// Declare the handler that will run on each error found
ValidationEventHandler xmlValidator =
new ValidationEventHandler(Xml_ValidationEventHandler);
// load the document
// will trhow XML.Exception if document is not "well formed"
doc.Load(new MemoryStream(f));
// Check if the required namespace is present
if (doc.DocumentElement.NamespaceURI == xmlNs) {
// Validate against xsd
// will call Xml_ValidationEventHandler on each error found
doc.Validate(xmlValidator);
if (xmlErrors.Rows.Count == 0) {
ret = "OK";
} else {
// return the complete error list, this is just to proove it works
ret = "File has " + xmlErrors.Rows.Count + " xml errors ";
ret += "when validated against our XSD.";
}
} else {
ret = "The xml document has incorrect or no namespace.";
}
} catch (XmlException ex) {
ret = "XML Exception: probably xml not well formed... ";
ret += "Message = " + ex.Message.ToString();
} catch (Exception ex) {
ret = "Exception: probably not XML related... "
ret += "Message = " + ex.Message.ToString();
}
return ret;
}
private void Xml_ValidationEventHandler(object sender, ValidationEventArgs e) {
xmlErrors.Rows.Add(new object[] { e.Severity, e.Message });
}
现在,xsd 会像这样:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="shiporder"
targetNamespace="http://mydomain.com/ns/upload.xsd"
elementFormDefault="qualified"
xmlns="http://mydomain.com/ns/upload.xsd"
xmlns:mstns="http://mydomain.com/ns/upload.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:simpleType name="stringtype">
<xs:restriction base="xs:string"/>
</xs:simpleType>
...
</xs:schema>
“好的”XML 应该是这样的:
<?xml version="1.0" encoding="utf-8" ?>
<shiporder orderid="889923" xmlns="http://mydomain.com/ns/upload.xsd">
<orderperson>John Smith</orderperson>
<shipto>
<names>Ola Nordmann</names>
<address>Langgt 23</address>
我测试过,“错误的 XML 格式”、“根据 XSD 的无效输入”、“不正确的命名空间”。
引用资料:
Trying avoid exception handling checking for wellformness
Validating against XSD, catch the errors
Interesting post about inline schema validation
嗨,马丁, 评论区太短了,所以我就在这里给出,它可能不是一个完整的答案,让我们一起改进它:)
我做了以下测试:
- 测试:xmlns="blaa"
- 结果:由于命名空间错误,文件被拒绝。
- 测试:xmlns="http://mydomain.com/ns/upload.xsd"和 xmlns:a="blaa"并且元素有 "a:someElement"
- 结果:文件返回错误,指出它不期望“a:someElement”
- 测试:xmlns="http://mydomain.com/ns/upload.xsd"和 xmlns:a="blaa"并且元素有“someElement”,缺少一些必需的属性
- 结果:文件返回错误,指出缺少该属性
遵循的策略(我更喜欢)是,如果文档不符合要求,则不接受,但提供一些信息说明原因(例如.“错误的命名空间”)。
此策略似乎与您之前所说的相反:
however, if a customer misses out the namespace declaration in their submitted XML then I would like to say that we can still validate it. I don't want to just say "You messed up, now fix it!"
在这种情况下,您似乎可以忽略 XML 中定义的命名空间。为此,您将跳过正确命名空间的验证:
...
// Don't Check if the required namespace is present
//if (doc.DocumentElement.NamespaceURI == xmlNs) {
// Validate against xsd
// will call Xml_ValidationEventHandler on each error found
doc.Validate(xmlValidator);
if (xmlErrors.Rows.Count == 0) {
ret = "OK - is valid against our XSD";
} else {
// return the complete error list, this is just to proove it works
ret = "File has " + xmlErrors.Rows.Count + " xml errors ";
ret += "when validated against our XSD.";
}
//} else {
// ret = "The xml document has incorrect or no namespace.";
//}
...
其他想法...
按照平行的思路,要用您自己的命名空间替换提供的命名空间,也许您可以设置 doc.DocumentElement.NamespaceURI = "mySpecialNamespace"
从而替换根元素的命名空间。
引用:
关于c# - 使用 XSD 验证 XML 时忽略提供的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8727915/