验证已经xs:import并具有schemaLocation的XML模式

In this example, Wsdl is a class that's one of our internal convenience wrappers around System.Web.Services.Description.ServiceDescription.  The Schemas property is just the XmlSchema[] that from service.Types.Schemas. 

在此示例中,Wsdl是一个类,它是我们围绕System.Web.Services.Description.ServiceDescription的内部便利包装之一。 Schemas属性只是service.Types.Schemas中的XmlSchema []。

I wasn't sure if my WSDL file was kosher, and I wanted to "validate" the whole setup.  Most of my XML Schemas were pulled in via xsd:import in my wsdl like:

我不确定我的WSDL文件是否是犹太洁食,我想“验证”整个设置。 我的大多数XML模式都是通过wsdl中的xsd:import插入的,例如:

 <wsdl:types>
  <xsd:schema targetNamespace="
http://www.corillian.com/thingie/operations/2199/05" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsd1="http://www.corillian.com/thingie/operations/2199/05"> <!-- bogus namespaces for example -->
   <xsd:import namespace="
http://www.corillian.com/thingie/banking/domain/2199/05" schemaLocation="..\banking\BankingDomain.xsd"/>
  </xsd:schema>  
 </wsdl:types>

<wsdl:types> <xsd:schema targetNamespace =“ http://www.corillian.com/thingie/operations/2199/05 ” xmlns:SOAP-ENC =“ http://schemas.xmlsoap.org/soap/encoding/ ” xmlns:xsd =“ http://www.w3.org/2001/XMLSchema ” xmlns:xsd1 =“ http://www.corillian.com/thingie/operations/2199/05 ”> <!-例如虚假的名称空间- > <xsd:import namespace =“ http://www.corillian.com/thingie/banking/domain/2199/05 schemaLocation =“ .. \ banking \ BankingDomain.xsd” /> </ xsd:schema> </ wsdl:types>

...and as you can see, the schemaLocation was relative.  The way I was going to force a Validation of the XML Schemas (other than physically loading each of them into memory myself) was to call .Compile().  However, it appears that the relative paths were being resolved relative to the Current AppDomain's current directory, NOT the directory the WSDL was located in!  So:

...正如您所看到的,schemaLocation是relative 。 我要强制进行XML模式验证的方法(而不是自己亲自将每个模式加载到内存中)是调用.Compile()。 但是,似乎相对路径是相对于当前AppDomain的当前目录(而不是WSDL所在的目录)进行解析的! 所以:

Wsdl w = new Wsdl();w.Load(file.FullName);foreach(XmlSchema x in w.Schemas)
{foreach(XmlSchemaImport i in x.Includes)
  {
i.SchemaLocation = Path.Combine(file.DirectoryName,i.SchemaLocation);  }
  x.Compile(new ValidationEventHandler(OnValidationEvent), new XmlUrlResolver());
}

Wsdl w =新的Wsdl(); w.Load(file.FullName); 的foreach(w.Schemas的XmlSchema x)的{ 的foreach(XmlSchemaImport ix.Includes) { i.SchemaLocation = Path.Combine(file.DirectoryName,i.SchemaLocation); } x.Compile( new ValidationEventHandler(OnValidationEvent), new XmlUrlResolver()); }

Before I mess with the WSDL and Schemas, I take the directory that the WSDL file was located in and use it as a base directory for the relative schemaLocation.  Thank goodness the SchemaLocation property was not readonly! :)  Using Path.Combine has the added benefit of collapsing ".." relative paths.

在弄乱WSDL和模式之前,请先获取WSDL文件所在的目录,并将其用作相对schemaLocation的基本目录。 谢天谢地,SchemaLocation属性不是只读的! :)使用Path.Combine具有折叠“ ..”相对路径的附加好处。

翻译自: https://www.hanselman.com/blog/validating-xml-schemas-that-have-been-xsimported-and-have-schemalocation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值