C# 用Linq解析异常XML

C# 用Linq解析异常XML,c#,wcf,linq-to-xml,webclient,C#,Wcf,Linq To Xml,Webclient,我从一家网络服务公司得到了一个特别的回复: <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> <GetLemonadeResponse xmln

我从一家网络服务公司得到了一个特别的回复:

<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>
    <GetLemonadeResponse xmlns="http://microsoft.com/webservices/">
      <GetLemonadeResult>&lt;Response&gt;&lt;Status&gt;Error&lt;/Status&gt;&lt;Message&gt;Could not find the Lemonade for this State/Lemon&lt;/Message&gt;&lt;FileNumber /&gt;&lt;/Response&gt;</GetLemonadeResult>
    </GetLemonadeResponse>
  </soap:Body>
</soap:Envelope>
(WebClient Tex源于具有额外超时属性的WebClient)

我在想,如果我选择了错误的编码,响应的外部部分也会以同样的方式被破坏

web服务是否有错误

2) 为什么当我尝试使用LINQtoXML获取“GetLemonadeResult”时,它什么都拉不出来

var xdoc = XDocument.Parse(response); // returns the XML posted above
var responseAsXML = xdoc.Descendants("GetLemonadeResult"); // gets nothing
我想不到我需要一个名称空间来捕获子体,因为XML GetLemonadeResult标记没有前置的“标记:”。

1)一些可以使XML无效的字符被转义,比如etc

2)您忘记在代码中包含名称空间

var xdoc = XDocument.Parse(response);
XNamespace soap = "http://schemas.xmlsoap.org/soap/envelope/";
XNamespace ns = "http://microsoft.com/webservices/";
var responseAsXML = xdoc.Descendants(soap + "Body")
                        .Descendants(ns + "GetLemonadeResult")
                        .First().Value;
响应XML

<Response>
<Status>Error</Status>
<Message>Could not find the Lemonade for this State/Lemon
</Message><FileNumber />
</Response>

错误
找不到此状态的柠檬水/柠檬
编辑

这是我用来测试的soap/xml

string response = @"<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>
                        <GetLemonadeResponse xmlns=""http://microsoft.com/webservices/"">
                            <GetLemonadeResult>&lt;Response&gt;&lt;Status&gt;Error&lt;/Status&gt;&lt;Message&gt;Could not find the Lemonade for this State/Lemon&lt;/Message&gt;&lt;FileNumber /&gt;&lt;/Response&gt;</GetLemonadeResult>
                        </GetLemonadeResponse>
                        </soap:Body>
                    </soap:Envelope>";
string-response=@”
ResponseStatusError/StatusMessage找不到此状态的柠檬水/Lemon/MessageFileNumber//Response
";

我在您的示例响应中没有看到“GetClosingProtectionLetterResult”?
GetClosingProtectionLetterResult
在您提供的示例XML中不存在。请用包含您的问题的XML更新您的问题。@cgtian:掩盖我来自哪个行业就到此为止!抱歉…@micahhoover我在你的问题中使用了soap/xml,它返回了我发布的xml。我编辑了答案。谢谢。我犯了一个愚蠢的错误。信封不需要显式“下降”。
string response = @"<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>
                        <GetLemonadeResponse xmlns=""http://microsoft.com/webservices/"">
                            <GetLemonadeResult>&lt;Response&gt;&lt;Status&gt;Error&lt;/Status&gt;&lt;Message&gt;Could not find the Lemonade for this State/Lemon&lt;/Message&gt;&lt;FileNumber /&gt;&lt;/Response&gt;</GetLemonadeResult>
                        </GetLemonadeResponse>
                        </soap:Body>
                    </soap:Envelope>";