Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8476145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:05:56+00:00 2026-06-10T18:05:56+00:00

I’m on my first forray into ASP.NET Web API and I’ve got a problem

  • 0

I’m on my first forray into ASP.NET Web API and I’ve got a problem which is causing me a great deal of frustration.

The RESTful service that I’m calling returns data such as:

<SearchResults  xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Results>
        <Result>
            <Title>foo</Title>
        </Result>
        <Result>
            <Title>bar</Title>
        </Result>
    </Results>
    <NumberOfResults>2</NumberOfResults>
</SearchResults>

So I’ve created some entities to represent this data:

[Serializable, DataContract(Namespace = "")]
public class SearchResults
{
    public List<Result> Results { get; set; }
    public int NumberOfResults { get; set; }
}

[Serializable, DataContract(Namespace = "")]
public class Result
{
    public string Title { get; set; }
}

And then I attempted to deserialize the data:

var client = new HttpClient();
client.BaseAddress = new Uri("some uri");

var response = client.GetAsync("/some/path/").Result;
response.EnsureSuccessStatusCode();

var result = response.Content.ReadAsAsync<SearchResults>().Result;

Which results in a SearchResults object with Results = null and NumberOfResults = 0 even though I know that there are results being returned, and if I check response.Content.ReadAsStringAsync(), the XML contains the results as expected.

I instead tried:

var serializer = new XmlSerializer(typeof(SearchResults));
var results = (SearchResults)serializer.Deserialize(response.Content.ReadAsStreamAsync().Result);

And that returned a fully populated SearchResults object.

Finally, I tried implementing a simple IFormatterLogger and passing that into ReadAsAsync, which got called if I tried to read the stream twice (as expected!), but doesn’t get called during a standard attempt at deserialization using ReadAsAsync.

I could just use an XmlSerializer and pass it the steam since that works, but that doesn’t seem as neat and tidy as using ReadAsAsync, plus I really want to know what I’m doing wrong 🙂

Updated 16:38BST 03/09/2012

Ok, I’m clearly missing something of vital importance when using Web API, but I still dont know what!

I’ve now tried consuming a service with a PUT verb, and whilst my entity serializes properly when I tested it using XmlSerializer the request was failing with an HTTP 500. I checked what was being sent with Fiddler and the XML generated by Web API looks nothing like that generated by XmlSerializer. For a start, everthing has names like “_x003C_Name_x003E_k__BackingField” rather than simply “Name”.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:05:58+00:00Added an answer on June 10, 2026 at 6:05 pm

    The weird “k__BackingField” names from my update have led me to the resolution, and as usual it was something very simple.

    I’d forgotten to mark the properties in the entities with the DataMemberAttribute.

    Marked them up and it all started working as I originally expected it to!

    That was all it was!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am currently running into a problem where an element is coming back from
I'm making a simple page using Google Maps API 3. My first. One marker
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.