繁体   English   中英

将StreamContent从HttpResponseMessage转换为XML

[英]StreamContent from HttpResponseMessage into XML

我正在从具有此代码的WebApi控制器调用现有的get方法(我无法对其进行修改)

   [HttpGet]
    public HttpResponseMessage Get()
    {
        XmlDataDocument xmldoc = new XmlDataDocument();
        FileStream fs = new FileStream("d:\\document.xml", FileMode.Open, FileAccess.Read);
        xmldoc.Load(fs);
        string str = xmldoc.DocumentElement.InnerXml;
        return new HttpResponseMessage() { Content = new StringContent(str, Encoding.UTF8, "application/xml") };

    }

我一直在尝试阅读这样的信息

            HttpClient client = new HttpClient();
            HttpResponseMessage response = client.GetAsync("http://localhost/api/info");
            HttpContent content = rm.Content;

我得到了StreamContent,但是我现在想做的是读取此内容,然后尝试将其反序列化为Xml Document以读取节点。

如何从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中的等待状态。

注意:代码尚未经过测试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM