2011-12-23 57 views
1
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
xmlns:tns="http://ttdev.com/ss" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy" 
xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" 
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd" 
name="SecureService" targetNamespace="http://ttdev.com/ss"> 


<wsp:Policy wsu:Id="p1"> 
<sp:SignedParts> 
<sp:Body /> 
</sp:SignedParts> 
</wsp:Policy> 

<wsdl:binding name="SecureServiceSOAP" type="tns:SecureService"> 

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" /> 

<wsdl:operation name="concat"> 
<wsp:PolicyReference URI="#p1" wsdl:required="true" /> 
<soap:operation soapAction="http://ttdev.com/ss/concat" /> 
<wsdl:input> 
<soap:body parts="concatRequest" use="literal" /> 
</wsdl:input> 
<wsdl:output> 
<soap:body parts="concatResponse" use="literal" /> 
</wsdl:output> 
</wsdl:operation> 

</wsdl:binding> 

<wsdl:service name="SecureService"> 
<wsdl:port binding="tns:SecureServiceSOAP" name="SecureServiceSOAP"> 
<soap:address location="http://localhost:8080/axis2/services/SecureService" /> 
</wsdl:port> 
</wsdl:service> 
</wsdl:definitions> 

这个WSDL包含一个政策部分和操作ANME和标签WSP的URI属性的基础上的操作部分 所以:PolicyReference 我想取整政策XML部件从这个WSDL解析政策,通过LINQ到XML

<wsp:Policy wsu:Id="p1"> 
    <sp:SignedParts> 
    <sp:Body /> 
    </sp:SignedParts> 
    </wsp:Policy> 

可以有很多政策,但其ID与政策refence其操作的名字我通过,这项政策我想的URI值相匹配。

你可以帮我取得政策的XML部分。

pOperationName变量CONCAT然后输出字符串的某一个合格值应如下:

<wsp:Policy wsu:Id="p1"> 
<sp:SignedParts> 
<sp:Body /> 
</sp:SignedParts> 
</wsp:Policy> 

使用做了下面的代码

private string GetPolicy() 
     { 
      XDocument wsdlDocument = XDocument.Load(_wsdlPath); 

      XName operationElementName = XName.Get("operation", "http://schemas.xmlsoap.org/wsdl/"); 
      XName policyReferenceElementName = XName.Get("PolicyReference", "http://schemas.xmlsoap.org/ws/2004/09/policy"); 
      XName policyElementName = XName.Get("Policy", "http://schemas.xmlsoap.org/ws/2004/09/policy"); 
      XName idAttributeName = XName.Get("Id", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd"); 

      var operationPolicy = from operation in wsdlDocument.Descendants(operationElementName) 
            where operation.Attribute("name").Value == _operationSelected 
            from policyReference in operation.Descendants(policyReferenceElementName) 
            where policyReference.Attribute("URI").Value.StartsWith("#") 
            let uri = policyReference.Attribute("URI").Value.Substring(1) 
            from policy in wsdlDocument.Descendants(policyElementName) 
            where policy.Attribute(idAttributeName).Value == uri    
            select policy.ToString(); 

      #region Removing Embedded Namespaces 
      string temp = operationPolicy.FirstOrDefault(); 
      if (temp.Contains(Constants.WSPolicyNsURI.XMLNS_SP) || temp.Contains(Constants.WSPolicyNsURI.XMLNS_WSP) || temp.Contains(Constants.WSPolicyNsURI.XMLNS_WSU)) 
      { 
       temp = temp.Replace(Constants.WSPolicyNsURI.XMLNS_SP, String.Empty); 
       temp = temp.Replace(Constants.WSPolicyNsURI.XMLNS_WSP, String.Empty); 
       temp = temp.Replace(Constants.WSPolicyNsURI.XMLNS_WSU, String.Empty); 
      } 

      #endregion 
      return temp; 
     } 
+0

主要是我为askign关于这部分//代码在URI变量 – Abhi 2011-12-23 15:23:11

回答

2
private string GetPolicy(string pWsdlPath, string pOperationName) 
    { 
     XDocument wsdlDocument = XDocument.Load(pWsdlPath); 

     XName operationElementName = XName.Get("operation", "http://schemas.xmlsoap.org/wsdl/"); 
     XName policyReferenceElementName = XName.Get("PolicyReference", "http://schemas.xmlsoap.org/ws/2004/09/policy"); 
     XName policyElementName = XName.Get("Policy", "http://schemas.xmlsoap.org/ws/2004/09/policy"); 
     XName idAttributeName = XName.Get("Id", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd"); 

     var operationPolicy = from operation in wsdlDocument.Descendants(operationElementName) 
           where operation.Attribute("name").Value == "concat" 
           from policyReference in operation.Descendants(policyReferenceElementName) 
           where policyReference.Attribute("URI").Value.StartsWith("#") 
           let uri = policyReference.Attribute("URI").Value.Substring(1) 
           from policy in wsdlDocument.Descendants(policyElementName) 
           where policy.Attribute(idAttributeName).Value == uri 
           select policy.ToString(); 

     return operationPolicy.FirstOrDefault(); 
    } 
+1

+1,但我会使用XNamespace – 2011-12-23 18:24:36

+0

@JohnSaunders我调用的方法没有XNamespace参数,但是我重构了XNames的元素有命名空间。从查询中获取名称空间似乎是一个很好的建议。 – JamieSee 2011-12-23 19:08:20

+0

如果您使用XNamespace +文本,它会生成一个XName。 – 2011-12-23 19:12:21

1

首先,为什么你用XmlDocument?直接使用XDocument.Load(pWsdlPath)

其次,您需要使用XNamespace

XNamespace wsdl = "http://schemas.xmlsoap.org/wsdl/"; 
XNamespace wsp = "http://schemas.xmlsoap.org/ws/2004/09/policy"; 

var objOperationsList = wsdlElement 
        .Elements(wsdl + "binding") 
        .Select(Operation => new 
        { 
         OperationName = (string)Operation.Element(wsdl + "operation").Attribute("name"), 
         OperationPolicyURI = (string)Operation.Element(wsdl + "operation").Element(wsp + "PolicyReference").Attribute("URI") 
        }); 
+0

的BAIS获取政策XML部分如何使用WHERE子句中这LINQ,这样只有一个名为“content”的操作会出现 – Abhi 2011-12-23 15:18:25