试图解决同样的问题。我想出了一个我认为是相当干净的解决方案。为了清楚起见,我省略了对输入参数的一些验证。
首先,场景:有一个接收文件的 Web 服务,该文件应该是“格式正确”的 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 输入无效”、“命名空间不正确”。
参考:
Read from memorystream
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”,但缺少一些必需的属性
- 结果:文件返回错误提示属性缺失
遵循的策略(我更喜欢)是,如果文件不符合要求,则不接受,但提供一些信息说明原因(例如.“错误的命名空间”)。
这个策略似乎与你之前所说的相反:
但是,如果客户在提交的 XML 中遗漏了命名空间声明,那么我想说我们仍然可以验证它。我不想只说“你搞砸了,现在解决它!”
在这种情况下,您似乎可以忽略 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" 从而替换根元素的名称空间。
参考:
add-multiple-namespaces-to-the-root-element