【发布时间】:2013-04-24 19:36:57
【问题描述】:
我从网络服务收到了一个特殊的响应:
<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><Response><Status>Error</Status><Message>Could not find the Lemonade for this State/Lemon</Message><FileNumber /></Response></GetLemonadeResult>
</GetLemonadeResponse>
</soap:Body>
</soap:Envelope>
2 个问题:
1) 我不知道为什么 GetLemonadeResult 的内容有异常内容(如“& l t ;”)。
我以这种方式将字节迁移到字符串:
WebClientEx client = new WebClientEx();
client.Headers.Add(HttpRequestHeader.ContentType, "text/xml; charset=utf-8");
client.Encoding = Encoding.UTF8;
byte[] result = client.UploadData(_baseUri.ToString(), data);
client.Encoding.GetBytes(xml));
string resultString = client.Encoding.GetString(result);
(WebClientEx 派生自带有额外 Timeout 属性的 WebClient)。
我在想,如果我选择了错误的编码,响应的外部部分也会以同样的方式被破坏。
网络服务有错误吗?
2) 为什么当我尝试使用 Linq to XML 抓取“GetLemonadeResult”时,它无法提取任何内容?
var xdoc = XDocument.Parse(response); // returns the XML posted above
var responseAsXML = xdoc.Descendants("GetLemonadeResult"); // gets nothing
我没想到我需要一个命名空间来捕获后代,因为 XML GetLemonadeResult 标记没有前置“标记:”。
【问题讨论】:
-
我在您的示例响应中没有看到“GetClosingProtectionLetterResult”?
-
GetClosingProtectionLetterResult在您提供的示例 XML 中不存在。请使用包含它的 XML 更新您的问题。 -
@cgtian:为了掩饰我来自哪个行业!道歉...
标签: c# wcf linq-to-xml webclient