2012-01-04 107 views
0

獲取XML在下面的WSDL(XML)通過LINQ

<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> 

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

    <wsp:Policy wsu:Id="p3"> 
     <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> 

我想從XML

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

我正在寫followign LINQ查詢

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 policy in wsdlDocument.Descendants(policyElementName) 
            where policy.Attribute(idAttributeName).Value == uritemp //uritemp = "p1" 
            select policy.ToString(); 

      string resultingXML = operationPolicy.FirstOrDefault(); 

下面的XML部分抓取但它不起作用,請告訴我我在哪裏犯錯。

+1

發佈的代碼似乎工作正常。你能詳細說明你所看到的錯誤/異常/問題嗎? – afrischke 2012-01-04 09:17:41

+0

我發現XName中的錯誤idAttributeName = XName.Get(「Id」,「http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd」) ; id在我的xml中很小的情況下,在代碼中它是大寫的,如何使它不區分大小寫 – Abhi 2012-01-04 09:21:46

+0

Xml屬性區分大小寫(「Id」和「id」是兩個不同的屬性),所以我不以任何方式看你如何編寫代碼,不考慮這種情況... – afrischke 2012-01-04 09:35:50

回答

1
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;