3

I have a class

public class C
{
    public int Id { get; set; }
    public SqlXml Range { get; set; } // System.Data.SqlTypes.SqlXml
}

And the following code is used to read the data from web api.

    List<C> cs = null;
    var response = await client.GetAsync(url);
    if (response.IsSuccessStatusCode)
    {
        cs = await response.Content.ReadAsAsync<List<C>>(); // cs[..].Range is null
    }

The following is some sample Json file returned from the Web API.

[{"id":0,"range":{"isNull":false,"value":"<Range>....</Range>"}},
 {"id":1,"range":{"isNull":false,"value":"<Range>...</Range>"}},
 {"id":2,"range":{"isNull":false,"value":"<Range>....</Range>"}}]

However, the variable cs got the following values. The Id values are correct. But all the Ranges got null values?

0, null
1, null
2, null

The debugger shows cs[...].Range.IsNull is true.

6
  • Did you get the previous issue sorted out? Commented Feb 28, 2018 at 1:22
  • Can you show definition of SqlXml class. maybe json.net is unable to desrialize that object, but will need to see what it looks like. Commented Feb 28, 2018 at 1:24
  • SqlXml is in System.Data.SqlTypes. I didn't define it. Commented Feb 28, 2018 at 1:26
  • Ok I'll go take a look at it. Commented Feb 28, 2018 at 1:26
  • those properties are read only. Commented Feb 28, 2018 at 1:32

1 Answer 1

2

The properties of System.Data.SqlTypes.SqlXml are read only. When the deserializer tries to create objects of that type it will fail while trying to set the properties.

Create you own class to hold the desired values

public class Range {
    public bool IsNull { get; set; }
    public string Value { get; set; }
}

public class C {
    public int Id { get; set; }
    public Range Range { get; set; }
}

This should now allow the Range values to be properly populated

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.