【问题标题】:Parsing unusual XML with Linq使用 Linq 解析异常的 XML
【发布时间】: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>&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>

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


【解决方案1】:

1) 转义了一些可以使 xml 无效的字符,例如 等

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;

responseAsXML 会是

<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>";

【讨论】:

  • @micahhoover 我在你的问题中使用了soap/xml,它返回了我发布的xml。我编辑了答案。
  • 谢谢。我最终犯了一个愚蠢的错误。信封不需要明确“下降”。
猜你喜欢
  • 2012-07-02
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 2012-01-20
  • 1970-01-01
  • 2012-05-29
  • 1970-01-01
  • 2014-05-16
相关资源
最近更新 更多