Mar*_*tin 12 c# xml xsd-validation
背景:
我们正在构建一个应用程序,允许我们的客户以预定义(即我们无法控制)的XML格式提供数据.XSD由第三方提供给我们,我们期望在我们处理它之前收到一个传递模式验证的XML文件.
问题:
我们提供的XSD包括默认和目标命名空间,这意味着如果客户提供的XML文件不包含命名空间,则验证将通过.我们显然不希望他们提供说他们通过但不应该提供的东西,但更大的问题是如果我找不到解决办法,我们需要对每个元素进行额外的检查. XML验证.
问题:
是否可以强制.NET执行验证并忽略提供的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>
Run Code Online (Sandbox Code Playgroud)
与命名空间不同
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns="urn:myCompany.com:blahExample1" attr1="2001-03-03" attr2="google" >
...
</root>
Run Code Online (Sandbox Code Playgroud)
没有命名空间.
<?xml version="1.0" encoding="UTF-8" ?>
<root attr1="2001-03-03" attr2="google" >
...
</root>
Run Code Online (Sandbox Code Playgroud)
小智 6
试图解决同样的问题.我想出了一个相当干净的解决方案.为清楚起见,我已经对输入参数进行了一些验证.
首先,场景:有一个Web服务接收一个文件,该文件应该是"格式良好"的xml并且对XSD有效.当然,我们不相信"好公",也不相信XSD对"我们知道"是正确的.
这种web服务方法的代码如下所示,我认为这是不言自明的.
主要的兴趣点是验证发生的顺序,你没有在加载之前检查名称空间,你检查之后,但干净利落.
我决定我可以忍受一些异常处理,因为它预计大多数文件将是"好的",因为这是交易的框架方式(所以我不会打它).
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 });
}
Run Code Online (Sandbox Code Playgroud)
现在,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>
Run Code Online (Sandbox Code Playgroud)
而"好"的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>
Run Code Online (Sandbox Code Playgroud)
我测试过,"格式错误的XML","根据XSD的输入无效","不正确的命名空间".
引用:
嗨马丁,评论部分对我的答案来说太短了,所以我会在这里给它,它可能或不是一个完整的答案,让我们一起改进:)
我做了以下测试:
所遵循的策略(我更喜欢)是,如果文档不符合,则不接受,但提供一些原因信息(例如"错误的命名空间").
这个策略似乎与你之前所说的相反:
但是,如果客户在提交的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.";
//}
...
Run Code Online (Sandbox Code Playgroud)
其他想法......
在一个平行的思路中,为了替换你自己提供的命名空间,也许你可以设置doc.DocumentElement.NamespaceURI = "mySpecialNamespace"这样替换根元素的namepsace.
参考:
| 归档时间: |
|
| 查看次数: |
11673 次 |
| 最近记录: |