My webapi method is:
public JsonResult<List<MyClass>> PullData()
{
List<MyClass> data = new List<MyClass>();
data = db.TableName.Select(x => new MyClass
{
Id = x.Id,
IsActive = x.IsActive,
//other attribute..
}).ToList();
return Json(data);
}
And I'm consuming this webapi as:
public async Task<string> Index()
{
string apiUrl = "http://localhost:90/api/Scheduler/pulldata";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(apiUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
JsonConvert.DeserializeXmlNode(data, "root"); //exception: XmlNodeConverter can only convert JSON that begins with an object.
}
}
return "Error";
}
I get error:
XmlNodeConverter can only convert JSON that begins with an object.
Also In api consumption method (i.e, Index
) when I debug and see data in var data = await response.Content.ReadAsStringAsync();
as JSON Visualizer
it shows data fine.
But when I do XML visualizer, it doesn't show data.
Update: The data is too large. I can't share it. Here is the screen shot of data.
Update 2:
Here is a part of json data from begining:
[{"LearningActivityKey":2122,"ModuleName":"certName","ModuleVersion":1.0,"ModuleDescription":"<p><span style=\"background-color:rgb(240, 240, 240); font-family:archivo narrow,helvetica,arial,sans-serif; font-size:16px; line-height:20px; white-space:pre-line\">Learn SAP
Update 3:
I have changed the webapi method PullData()
to send only two records, so that we can easily visualize wethere the issue is with json data.
Complete data is:
[{"LearningActivityKey":2122,"ModuleName":"certName","ModuleVersion":0.0,"ModuleDescription":null,"BadgeName":null,"BadgeVersion":null,"BadgeDescription":null,"MozillaBadge":null,"LearningActivityName":null,"LearningActivityDescription":null,"StepName":null,"StepVersion":null,"StepDescription":null,"IsActive":false,"IsPublished":false,"CreatedDate":"0001-01-01T00:00:00","ModifiedDate":null},{"LearningActivityKey":2122,"ModuleName":"certName","ModuleVersion":0.0,"ModuleDescription":null,"BadgeName":null,"BadgeVersion":null,"BadgeDescription":null,"MozillaBadge":null,"LearningActivityName":null,"LearningActivityDescription":null,"StepName":null,"StepVersion":null,"StepDescription":null,"IsActive":false,"IsPublished":false,"CreatedDate":"0001-01-01T00:00:00","ModifiedDate":null}]
I pasted data in https://jsonformatter.curiousconcept.com/ and it says:
And XML Visualizer
still doesn't show any data.
The exception is self-explanatory: you cannot convert JSON to XML unless the root token is an object, even if you use the
JsonConvertDeserializeXmlNode(String, String)
method to specify an outer root element name.As to why this is true, the documentation page Converting between JSON and XML shows that a JSON array is converted to a repeating sequence of XML elements without an added outer container element. I.e. JSON like this (simplified from the documentation):
Is converted to XML as follows:
Notice that an outer
<root>
node is created, and a repeated sequence of<person>
nodes -- but nothing in between? If there were no outer object with a"root"
property in the JSON then Json.NET would have tried to create XML with multiple<person>
root elements. This is disallowed by the XML standard which requires exactly one root element. Thus it appears a JSON array must be contained within at least two levels of JSON object nesting to be successfully converted to XML (although one of those levels could come by specifying an outer root element name viaJsonConvertDeserializeXmlNode(String, String)
).As a workaround, you can introduce the following extension methods to nest your JSON in an extra level of object.
First, grab
ChainedTextReader
andpublic static TextReader Extensions.Concat(this TextReader first, TextReader second)
from the answer to How to string multiple TextReaders together? by Rex M. Using them, create the following extension methods:And convert to XML as follows:
Using the JSON from your question, the following XML is generated:
Working sample .Net fiddle.