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>

This WSDL contains a policy section and an operation section So on the basis of the operation anme and the URI attribute of Tag wsp:PolicyReference I want to fetch the whole Policy XMl part from this WSDl

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

There can be many policy but whose ID matched with URI value of Policy refence whose operation name i pass , that policy I want .

can u help me to fetch policy XML part.

When some one pass value of pOperationName variable as concat then output string should be following:

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

Done it using following Code

 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;
        }
Abhi
  • 5,501
  • 17
  • 78
  • 133
  • Mainly I m askign about this part //Code to fetch Policy XML part on the bais of uri variable – Abhi Dec 23 '11 at 15:23

2 Answers2

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();
    }
Abhi
  • 5,501
  • 17
  • 78
  • 133
JamieSee
  • 12,696
  • 2
  • 31
  • 47
  • @JohnSaunders The methods that I called don't have an XNamespace argument, but I did refactor out XNames for the elements that have namespaces. Getting the namespaces out of the query seemed like a good suggestion. – JamieSee Dec 23 '11 at 19:08
  • If you use XNamespace + text, it produces an XName. – John Saunders Dec 23 '11 at 19:12
  • The function is giving following output – Abhi Dec 23 '11 at 19:14
  • why it is adding this xmlns:wsu="docs.oasis-open.org/wss/2004/01/…; xmlns:wsp="schemas.xmlsoap.org/ws/2004/09/policy">; as extra text which is not wanted , although your query is superb . good work :) – Abhi Dec 23 '11 at 19:16
  • it should render only this as output – Abhi Dec 23 '11 at 19:17
  • I m about to mark it as answered as it solves 99% problem , just want to know why it is rendering two extra namesapces in the resulting string – Abhi Dec 23 '11 at 19:29
  • @AbhishekGupta The wsu namespace is included because the id attribute on the Policy element uses it: . The wsp namespace is included because the Policy element uses it. The reason they show up in the result is that the ToString method on XElement insists on writing well-formed XML which means no prefixes without an attached namespace are allowed. – JamieSee Dec 28 '11 at 16:31
  • @James It is not working if I m having multiple Wp:policy tags, can u plz tell what to change in this code to achieve this – Abhi Jan 04 '12 at 07:09
  • @AbhishekGupta You could try changing the return type to List and make the return statement return operationPolicy.ToList();. – JamieSee Jan 05 '12 at 23:12
  • @AbhishekGupta If my prior comment doesn't give you what you're after, post a sample of your Xml with the multiple declarations and the output you want, like you did before, and I'll take a look at it. – JamieSee Jan 06 '12 at 17:54
1

First of all, why are you using XmlDocument? Just use XDocument.Load(pWsdlPath) directly.

Secondly, you need to use 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")
                  });
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • how to use where clause in this LINQ so that only that one operation whose name is "content" will come – Abhi Dec 23 '11 at 15:18