web api下载xml + HttpResponseMessage

时间:2014-05-28 06:31:40

标签: c# xml asp.net-web-api

我使用web api返回xml文件中的项目列表。它有效。

这是我的方法:

/// <summary>
/// Gets the product by division identifier.
/// </summary>
/// <param name="id">The div identifier.</param>
/// <param name="userId">The user identifier.</param>
/// <returns>A Xml file flow</returns>
[ActionName("Divisionxml")]
public HttpResponseMessage GetProductByDivisionIdXml(int id, int userId)
{
    var result = new HttpResponseMessage(HttpStatusCode.OK);
    var adoria = new Adoria(....);
    var serializer = new XmlSerializer(typeof (Adoria));

    var builder = new StringBuilder();
    using (var writer = new StringWriter(builder))
    {
        serializer.Serialize(writer, adoria);

        result.Content = new StringContent(builder.ToString(), Encoding.UTF8, "application/xml");
        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                                                                {
                                                                    FileName =
                                                                        string.Format("export_product_{0}.xml", id)
                                                                };

        return result;
    }

    return new HttpResponseMessage();
}

上传xml文件并由浏览器自动下载。尼斯。 但exml文件中有第一行:

<?xml version="1.0" encoding="utf-16"?>

我不知道为什么。我允许直接在浏览器上显示,这条线不会出现......

你有什么建议吗?

0 个答案:

没有答案