【问题标题】:How to modify an MVC Get to return an object that works with XDocument.Load如何修改 MVC Get 以返回与 XDocument.Load 一起使用的对象
【发布时间】:2016-06-28 18:10:07
【问题描述】:

我正在使用第三方开发的 Windows 8/8.1 应用程序,该应用程序依赖于 Web 服务来检索数据。我们有应用程序的源代码,但我们没有用于 Web 服务的源代码,所以我需要重新构建它。更复杂的是,我无法对应用程序进行更改,因为它已在许多设备上侧载。

尽管我能够反编译 Web 服务的 DLL,但它并没有让我进入工作点。现在的障碍似乎是 XDocument.Load 无法处理返回的流。

这是应用程序代码:

    public async Task<CustomerModel> ReadDataFromXml(string address)
    {
        var client = new HttpClient();
        var response = await client.GetAsync(address);

        // check that response was successful or throw exception
        response.EnsureSuccessStatusCode();

        var streamResponse = await response.Content.ReadAsStreamAsync();
        var xDocumentObject = XDocument.Load(streamResponse);

        var contents = from contact in xDocumentObject.Descendants(Constants.CUSTOMER_TAG)
                       select new { 
                       ...

我正在使用 VS 2013 使用内置的 MVC ASP.NET Web 应用程序模板构建服务,该模板实现了 ApiController。

旧代码,用UTF8Encoding序列化后返回结果通过

HttpContext.Current.Response.Write(strMessage);

新代码

    public IEnumerable<Customer> Get(string activationcode)
    {
        return _handler.GetCustomerData(activationcode);
    }

我相信我需要通过当前返回 IEnumerable 的 Get 调用返回它。

我相信我尝试为 Get 调用返回一个字符串

我尝试的另一件事是将 Get 语句更改为返回一个字符串,然后返回文本的 XML 序列化版本。

private string Serialize(Customer cus)
    {
        try
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Customer));
            XmlTextWriter xmlTextWriter = new XmlTextWriter((Stream)new MemoryStream(), Encoding.UTF8);
            xmlSerializer.Serialize((XmlWriter)xmlTextWriter, (object)cus);
            return this.UTF8ByteArrayToString(((MemoryStream)xmlTextWriter.BaseStream).ToArray());
        }
        catch (Exception ex)
        {
            return string.Empty;
        }
    }

但返回“根级别的数据无效,第 1 行,位置 1”

TLDR;有没有办法让 GET 语句返回结果,以便我们的 Web 应用程序中的 XDocument.Load 能够正常运行?

【问题讨论】:

  • 你能发布你从HttpClient得到的数据吗?也许是json?
  • 请检查这个 SO 答案:stackoverflow.com/questions/31115545/…
  • 这是文本返回。 "w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"w3.org/2001/XMLSchema\"> 64Chuck NorrisCNORRIS2016-02-10T18:00:000001-01-01T00:00:00 falsefalsefalsefalsefalse"
  • 其实我应该补充一下,这就是我要返回的字符串...它没有进入应用程序,我什至不确定这是我的问题。
  • 你能用你的浏览器调用web api,只需输入URL,你应该会看到返回的字符串。并且当你得到这个“根级别的数据无效,第1行,位置1”异常时,请写下你尝试解析的数据

标签: c# asp.net-mvc


【解决方案1】:

终于想通了:

不返回对象,而是将 Get 函数更改为返回 HttpReponseMessage。

public HttpResponseMessage Get(string activationcode)

序列化你的对象返回的数据

Customer customer = _handler.GetCustomerData(activationcode).First();
string serializedCustomer = Serialize(customer);

并使用 StringContent 构建您的回报:

return new HttpResponseMessage
{
    Content = new StringContent(serializedCustomer, Encoding.UTF8, "text/xml")
};

这是用于序列化数据的函数。

private string Serialize(Customer cus)
{
    try
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(Customer));
        XmlTextWriter xmlTextWriter = new XmlTextWriter((Stream)new MemoryStream(), Encoding.UTF8);
        xmlSerializer.Serialize((XmlWriter)xmlTextWriter, (object)cus);
        return this.UTF8ByteArrayToString(((MemoryStream)xmlTextWriter.BaseStream).ToArray());
    }
    catch (Exception ex)
    {
        return string.Empty;
    }
}


private string UTF8ByteArrayToString(byte[] characters)
    { 
        return new UTF8Encoding().GetString(characters);
        //return characters.ToString(); 
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-03-28
    相关资源
    最近更新 更多