简体   繁体   English

.Net Core - 使用 LINQ to XML 从 SOAP 响应中按名称获取元素

[英].Net Core - Use LINQ to XML to get element by name from SOAP response

I have found quite a few instances of this type of question asked but for some reason none of the answers in all of those questions have worked for me.我发现很多此类问题的例子,但由于某种原因,所有这些问题的答案都没有对我有用。

The SOAP response that I get is this very simple one:我得到的 SOAP 响应非常简单:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <GetDataAsStringResponse xmlns="http://tempuri.org/">
            <GetDataAsStringResult>true</GetDataAsStringResult>
        </GetDataAsStringResponse>
    </s:Body>
</s:Envelope>

And these are all the options that I have tried in order to parse it and get to the "GetDataAsStringResult" element:这些是我为解析它并获得“GetDataAsStringResult”元素而尝试的所有选项:

var doc = XDocument.Parse(responseString);

XNamespace xmlns = "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"";
XNamespace nsTempuri = "xmlns=\"http://tempuri.org/\"";
var namespacePrefix = doc.Root.GetNamespaceOfPrefix("s");

var resultElement = doc.Descendants("GetDataAsStringResult"); //yields no results
var resultElement1 = doc.Descendants(nsTempuri + "GetDataAsStringResult"); //yields no results
var resultElement2 = doc.Descendants(xmlns + "GetDataAsStringResult"); //yields no results
var resultElement3 = doc.Descendants(namespacePrefix + "GetDataAsStringResult"); //yields no results


var resultElement4 = doc.Descendants(namespacePrefix + "Body"); //Gets s:Body element with descendants

var resultElement5 = resultElement3.Descendants("GetDataAsStringResponse");  //yields no results
var resultElement6 = resultElement3.Descendants(nsTempuri + "GetDataAsStringResponse");  //yields no results
var resultElement7 = resultElement3.Descendants(namespacePrefix + "GetDataAsStringResponse");  //yields no results

var resultElement8 = resultElement4.Descendants(); //Gets IEnumerable with both GetDataAsStringResponse and GetDataAsStringResult

Based on all this experimentation I could do something like the following:基于所有这些实验,我可以执行以下操作:

var doc = XDocument.Parse(responseString);
var namespacePrefix = doc.Root.GetNamespaceOfPrefix("s");

var dataAsStringResult = from data in doc.Descendants(namespacePrefix + "Body")
                           let descendats = data.Descendants()
                           select descendats.TakeLast(1).Single();

That looks like too many steps to get to what I need, especially when according to quite some answers in SO I should be able to do it in a much simpler fashion.看起来要达到我需要的步骤太多了,特别是当根据 SO 中的相当多的答案时,我应该能够以更简单的方式做到这一点。

Any insights about why I am unable to get directly to the GetDataAsStringResult by using the "Descendants" method even when adding the namespace would be much appreciated.任何有关为什么即使在添加命名空间时我也无法通过使用“后代”方法直接访问 GetDataAsStringResult 的见解将不胜感激。

Thank you.谢谢你。

After a couple of hours of going crazy it turned out to be a "you missed the semicolon" issue.经过几个小时的疯狂后,结果证明是“您错过了分号”问题。 The namespace object should be declared without the "xlmns=" part as follows:命名空间对象应该在没有“xlmns=”部分的情况下声明,如下所示:

XNamespace nsTempuri = "http://tempuri.org/";

By doing that then the following works:通过这样做,然后以下工作:

doc.Descendants(nsTempuri + "GetDataAsStringResult");

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

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