复杂的LINQ查询从WSDL文件中获取策略

时间:2012-01-04 07:46:01

标签: c# .net linq soap wsdl

 <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="p0">
         <sp:SignedParts>
         <sp:Body />
         </sp:SignedParts>
        </wsp:Policy>

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

    <wsp:Policy wsu:Id="p2">
         <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包含策略部分和操作部分 因此,基于Tag wsp:PolicyReference 的操作名称和URI属性 我想从这个WSDl

中获取整个Policy XMl部分
<wsp:Policy wsu:Id="p1">
     <sp:SignedParts>
     <sp:Body />
     </sp:SignedParts>
    </wsp:Policy>

可以有许多策略但其ID与策略引用的URI值匹配,其操作名称为i,即我想要的策略。

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

当某个人将 pOperationName 变量的值传递为 concat 时,输出字符串应该如下:

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

使用以下代码完成它(但只有当存在一个wsp:policy标记时,此查询才有效如果WSDL中存在多个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 //"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();

           string temp = operationPolicy.FirstOrDefault();
            return temp;
        }

1 个答案:

答案 0 :(得分:0)

XDocument wsdlDocument = XDocument.Load(_wsdlPath);

        XName operationElementName = XName.Get(Constants.OPERATION, Constants.WSDL_NS);
        XName policyReferenceElementName = XName.Get(Constants.POLICY_REFERENCE, Constants.NamespacePath.POLICY);
        XName policyElementName = XName.Get(Constants.POLICY, Constants.NamespacePath.POLICY);
        XName idAttributeName = XName.Get("Id", Constants.NamespacePath.WSSECURITY);


        var uriNo = from operation in wsdlDocument.Descendants(operationElementName)
                    where operation.HasAttributes && operation.Attribute(Constants.NAME).Value == _operationSelected
                    from policyReference in operation.Descendants(policyReferenceElementName)
                    where policyReference.HasAttributes && policyReference.Attribute(Constants.URI).Value.StartsWith(Constants.HASH)
                    select policyReference.Attribute(Constants.URI).Value.Substring(1);

        string uritemp = uriNo.FirstOrDefault().ToString();

        var operationPolicy = from policy in wsdlDocument.Descendants(policyElementName)
                              where policy.HasAttributes && policy.Attribute(idAttributeName).Value == uritemp
                              select policy;

        string temp = operationPolicy.FirstOrDefault().ToString();
        return temp;