我向演示 API 发送了一个 XML 帖子,响应以 XML 流的形式返回,如下所示:
API=3CProductData&XML=%3CProductData+Name%3D%22NameTest%22%3E%0D%0A++%3CId%3EXXXXXXXXX%3C%2FId%3E%0D%0A%3C%2FProductData%3E
我猜这就是流的样子,我的目标是获取该响应并将其存储在新的 ProductData 对象中,这是我迄今为止所做的:
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// as an xml: deserialise into your own object or parse as you wish
StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string receivedResponse = respStream.ReadToEnd();
XmlSerializer x = new XmlSerializer(typeof(ProductData));
ProductData product = (ProductData) x.Deserialize(new StringReader(receivedResponse));
Console.WriteLine("Node1: " + product.Id.ToString());
Console.WriteLine("Node2: " + product.Name);
Console.ReadKey();
}
错误返回时出现 System.InvalidOperationException:“XML 文档 (0, 0) 中存在错误。” XmlException:缺少根元素。
慕哥6287543
哔哔one
相关分类