将命名空间属性从 XML 移动到顶部

发布于 2024-12-23 05:47:07 字数 864 浏览 0 评论 0原文

你好,朋友们,我有以下类型的 XML

<wsp:Policy wsu:Id="p1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
  <sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
    <sp:Body />
  </sp:SignedParts>
</wsp:Policy>

,但我想将其转换为以下格式,

<wsdl:definitions
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>

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

</wsdl:definitions>

您能告诉我如何通过 LINQ 或任何其他 API 执行此操作吗

Hello friends I m having following type of XML

<wsp:Policy wsu:Id="p1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wsswssecurity-utility-1.0.xsd" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy">
  <sp:SignedParts xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
    <sp:Body />
  </sp:SignedParts>
</wsp:Policy>

But I want to convert it into following format

<wsdl:definitions
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>

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

</wsdl:definitions>

Can you please tell how to do this either through LINQ or any other API

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

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

发布评论

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

评论(1

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