在 SOAP 调用中排除结果元素



我正在尝试排除从我的 SOAP 调用中返回的结果元素,但我似乎无法将其排除。然后,我设置了这个从文件返回 xml 数据的简单调用。我需要删除结果元素,因为它不会针对架构进行验证。我尝试添加 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 样式表

最新更新