通过 LINQ to XML 解析策略

发布于 2024-12-22 18:54:28 字数 4039 浏览 1 评论 0原文

<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的操作以及Tag wsp:PolicyReference的URI属性 我想从此 WSDl 获取整个策略 XMl 部分。

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

可以有很多策略,但其 ID 与我传递的操作名称的策略引用的 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;
        }
<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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

素衣风尘叹 2024-12-29 18:54:28
    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();
    }
    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();
    }
攒一口袋星星 2024-12-29 18:54:28

首先,为什么要使用 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")
                  });

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")
                  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文