0

I'm trying to exclude the result element being returned from my SOAP call but I cant seem to get it excluded. I then set up this simple call which returns xml data from a file. I need the result element removed as it wont validate against the schema. I've tried adding the SoapParameterStyle.Bare but i still get the node

    [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;
    }

Here is the first few nodes that would be returned from that SOAP call and as you can see the TestMethodResult is returned under the Body element:

<?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>
user2661305
  • 311
  • 1
  • 4
  • 16

1 Answers1

0

You can run simple XSLT transformation on XML. Example program

        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();                        
                }
            }
        }

And the output at the end contains your xml (without TestMethodResult xmlns="http://tempuri.org/")

This answer was inspired by two other SOF questions:

How do I remove the outermost wrappers using xslt?

How to transform XML as a string w/o using files in .NET?

Also if you one to stick with the XML files loaded from disk you can do it like this:

        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();

compare: How to apply an XSLT Stylesheet in C#

Piotr
  • 1,155
  • 12
  • 29