简体   繁体   中英

Web API Serialize XML

I am trying to serialize XML using the web API, but I receive a null result.

Here is my XML:

http://pastebin.com/RxBPaZF5

Using XSD, I generated a class in Visual Studio:

http://pastebin.com/kUca0whm

Here is the code I am using:

public COM_Order GetOrderById(string orderId)
    {
        HttpClientHandler handler = new HttpClientHandler();
        handler.Credentials = new NetworkCredential(m_UserName, m_Password);

        HttpClient client = new HttpClient(handler)
        {
        };


        string url =  m_BaseUrl + String.Format("/rest/ecommerce.order/{0}", orderId);
        HttpResponseMessage response = client.GetAsync(url).Result;
        if (response.IsSuccessStatusCode)
        {
            data result = response.Content.ReadAsAsync<data>().Result;
            //result is null, even though valid JSON is returned
            return result.Items.FirstOrDefault();
        }

        return null;


    }

Is there a problem with my class? I can also return JSON.

It says response.Content. ReadAsAsync ().Result;

Are your sure result is populated before you return it? In the debugger you have one behaviour, outside another.

Try turning ReadAsAsync off to see if it is the culprit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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