Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将HttpResponseMessage中的内容转换为XML_C#_Xml_Asp.net Web Api_Stream - Fatal编程技术网

C# 将HttpResponseMessage中的内容转换为XML

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

我正在从包含此代码的WebApi控制器调用现有的get方法(我无法修改它)

我一直想这样读这些信息

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