繁体   English   中英

在SOAP调用中排除结果元素

[英]Exclude Result Element in SOAP Call

我试图排除从SOAP调用返回的结果元素,但似乎无法排除它。 然后,我设置了这个简单的调用,该调用从文件返回xml数据。 我需要删除result元素,因为它将无法针对架构进行验证。 我尝试添加SoapParameterStyle.Bare,但是我仍然得到该节点

    [WebMethod]
    [SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
    [return: System.Xml.Serialization.XmlElement(IsNullable = true)]
    public XmlDocument TestMethod(XmlDocument p_xmlDoc)
    {
        XmlDocument xmldoc = new XmlDocument();
        string sResponsePath = ConfigurationManager.AppSettings["FileDirectory"] + ConfigurationManager.AppSettings["GetOrders"];
        string sXMLResponse = File.ReadAllText(sResponsePath);
        xmldoc.LoadXml(sXMLResponse);
        return xmldoc;
    }

这是从该SOAP调用返回的前几个节点,并且您可以看到TestMethodResult在Body元素下返回:

<?xml version="1.0" encoding="utf-8"?>
  <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
      <TestMethodResult xmlns="http://tempuri.org/">
        <ns0:TestMethodResponse xmlns:ns0="http://eibdigital.co.uk/citb/EibOrderInterface">
          <ns0:TestMethodResult>

您可以在XML上运行简单的XSLT转换。 范例程序

        string xmlInput = @"<?xml version=""1.0"" encoding=""utf-8""?>
        <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
            <soap:Body>
              <TestMethodResult xmlns=""http://tempuri.org/"">
                <ns0:TestMethodResponse xmlns:ns0=""http://eibdigital.co.uk/citb/EibOrderInterface"">
                  <ns0:TestMethodResult>
                  </ns0:TestMethodResult>
                </ns0:TestMethodResponse>
                </TestMethodResult>
            </soap:Body>
        </soap:Envelope>";

        string xslInput = @"<?xml version=""1.0"" encoding=""UTF-8""?>
        <xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" 
        xmlns:tmp=""http://tempuri.org/""
        version=""1.0"">
            <!--identity template, copies all content by default-->
            <xsl:template match=""@*|node()"">
                <xsl:copy>
                    <xsl:apply-templates select=""@*|node()""/>
                </xsl:copy>
            </xsl:template>

            <!--don't generate content for the <b>, just apply-templates to it's children-->
            <xsl:template match=""tmp:TestMethodResult"">
                <xsl:apply-templates/>
            </xsl:template>     
        </xsl:stylesheet>";

        string output = String.Empty;
        using (StringReader srt = new StringReader(xslInput))
        using (StringReader sri = new StringReader(xmlInput))
        {
            using (XmlReader xrt = XmlReader.Create(srt))
            using (XmlReader xri = XmlReader.Create(sri))
            {
                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load(xrt);
                using (StringWriter sw = new StringWriter())
                using (XmlWriter xwo = XmlWriter.Create(sw, xslt.OutputSettings))
                {
                    xslt.Transform(xri, xwo);
                    output = sw.ToString();                        
                }
            }
        }

最后的输出包含您的xml(没有TestMethodResult xmlns =“ http://tempuri.org/”)

该答案的灵感来自其他两个SOF问题:

如何使用xslt删除最外面的包装?

如何使用.NET中的文件将XML转换为不带字符串的字符串?

另外,如果您坚持使用从磁盘加载的XML文件,则可以这样做:

        string sResponsePath = ConfigurationManager.AppSettings["FileDirectory"] + ConfigurationManager.AppSettings["GetOrders"];
        string xsltPath = "Drive://your/path/to/xslt/file.xslt";

        XPathDocument myXPathDoc = new XPathDocument(sResponsePath);
        XslCompiledTransform myXslTrans = new XslCompiledTransform();
        myXslTrans.Load(xsltPath);
        var ms = new MemoryStream();
        XmlTextWriter myWriter = new XmlTextWriter(ms, Encoding.UTF8);
        myXslTrans.Transform(myXPathDoc, null, myWriter);
        ms.Position = 0;
        var sr = new StreamReader(ms);
        var myStr = sr.ReadToEnd();

比较: 如何在C#中应用XSLT样式表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM