C# 将HttpResponseMessage中的内容转换为XML
我正在从包含此代码的WebApi控制器调用现有的get方法(我无法修改它) 我一直想这样读这些信息C# 将HttpResponseMessage中的内容转换为XML,c#,xml,asp.net-web-api,stream,C#,Xml,Asp.net Web Api,Stream,我正在从包含此代码的WebApi控制器调用现有的get方法(我无法修改它) 我一直想这样读这些信息 HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync("http://localhost/api/info"); HttpContent content = rm.Content; 我得到了一个StreamCo
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync("http://localhost/api/info");
HttpContent content = rm.Content;
我得到了一个StreamContent,但我现在喜欢做的是读取此内容并尝试将其反序列化为Xml文档,以便读取节点
如何从HttpContent的流内容中获取此信息
string response;
using(var http = new HttpClient())
{
response = await http.GetStringAsync("http://localhost/api/info");
}
var xml = new XmlDataDocument();
xml.LoadXml(response);
您可以使用GetStringAsync来获取字符串,而不是HttpContent对象。您还错过了GetAsync中的wait
注意:代码尚未测试谢谢您的回答。现在我得到了所有xml结构的响应(没有xml和名称空间,这没关系),但当我到达LoadXML时,它会抛出路径中非法字符的异常。我的字符串响应是员工的集合“Smith1990-09-15Scott”你得到的实际字符串是什么样子的?我试过了。它没有任何错误,这就是我问的原因。您能确认这正是调试时得到的吗?路径中的非法字符。对不起。我使用的是.Load而不是.LoadXml方法。非常感谢你。
string response;
using(var http = new HttpClient())
{
response = await http.GetStringAsync("http://localhost/api/info");
}
var xml = new XmlDataDocument();
xml.LoadXml(response);