【问题标题】:Parsing policy through LINQ to XML通过 LINQ to XML 解析策略
【发布时间】:2011-12-23 15:09:01
【问题描述】:
<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 中获取整个 Policy 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;
        }

【问题讨论】:

  • 我主要是问这部分 //Code to fetch Policy XML part on the uri variable

标签: c# .net linq wsdl linq-to-xml


【解决方案1】:
    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();
    }

【讨论】:

  • @JohnSaunders 我调用的方法没有 XNamespace 参数,但我确实为具有命名空间的元素重构了 XNames。从查询中取出命名空间似乎是一个不错的建议。
  • 如果你使用 XNamespace + text,它会产生一个 XName。
  • 函数给出以下输出 docs.oasis-open.org/wss/2004/01/…" xmlns:wsp="schemas.xmlsoap.org/ws/2004/09/policy"> schemas.xmlsoap.org/ws/2005/07/securitypolicy">
  • 为什么要添加这个 xmlns:wsu="docs.oasis-open.org/wss/2004/01/...; xmlns:wsp="schemas.xmlsoap.org/ws/2004/09 /policy">; 作为不需要的额外文本,尽管您的查询很棒。干得好:)
  • 它应该只呈现这个 作为输出
【解决方案2】:

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

【讨论】:

  • 如何在这个 LINQ 中使用 where 子句,这样只有一个名为“content”的操作才会出现
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-20
  • 2011-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多