I’m getting a response string from Azure DevOps REST API that contain mostly JSON, with some XML sprinkled into a few property values. I build a number of C# classes replicating the JSON structure of the response, in order to deserialize the response.
When
var response = client.Get(request);
if (response.StatusCode == HttpStatusCode.OK
&& response.Content != null)
{
return JsonSerializer.Deserialize<T>(response.Content);
}
reaches the XML in
public class Fields
{
...
[JsonPropertyName("Microsoft.VSTS.TCM.Steps")]
public Steps MicrosoftVSTSTCMSteps { get; set; }
}
I get a
System.Text.Json.JsonException: 'The JSON value could not be converted to ADO.Response.Steps. Path: $.fields['Microsoft.VSTS.TCM.Steps'] | LineNumber: 0 | BytePositionInLine: 5149.'
The Steps class is defined like
[XmlRoot(ElementName = "steps")]
public class Steps
{
[XmlElement(ElementName = "step")]
public Step[] Step { get; set; }
[XmlAttribute(AttributeName = "id")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "last")]
public string Last { get; set; }
...
}
An exerpt of the incomming JSON/XML string looks like this:
{
...
"Microsoft.VSTS.TCM.Steps":"<?xml version="1.0"?><steps last="5" id="0"><step id="2" type="ActionStep"><parameterizedString isformatted="true"></parameterizedString><description /></step></steps><xml/>
...
}
How do I deserialize the JSON and the XML without detaching the Steps class from the hierarchy by e.g keeping the XML as a string in the deserialized JSON?
I’m looking to avoid post processing like this
XmlSerializer XmlSerializer = new XmlSerializer(typeof(Steps));
Steps steps = (Steps)XmlSerializer.Deserialize(new StringReader(resp.fields.MicrosoftVSTSTCMSteps));
2
Answers
See solution below
It is (as the most json tasks too) much easier to do with Newtonsoft.Json
classes