I'm trying to convert JSON to XML. My JSON contains an array of cars and each car has an array of features:
[
{
"car": {
"features": [{
"code": "1"
}, {
"code": "2"
}]
}
},
{
"car": {
"features": [{
"code": "3"
}, {
"code": "2"
}]
}
}
]
I'm converting this to XML:
// the tag name for each top level element in the json array
var wrappedDocument = string.Format("{{ car: {0} }}", jsonResult);
// set the root tag name
return JsonConvert.DeserializeXmlNode(wrappedDocument, "cars");
This is the resulting XML:
<cars>
<car>
<features>
<code>1</code>
</features>
<features>
<code>2</code>
</features>
</car>
<car>
<features>
<code>3</code>
</features>
<features>
<code>2</code>
</features>
</car>
</cars>
My problem is that I would like to have all "features" listed under a common element just like "car" is listed under "cars" so that the XML would look like this:
<cars>
<car>
<features>
<feature>
<code>1</code>
</feature>
<feature>
<code>2</code>
</feature>
</features>
</car>
<car>
<features>
<feature>
<code>3</code>
</feature>
<feature>
<code>2</code>
</feature>
</features>
</car>
</cars>
Is that possible using Newtonsoft Json.NET? Thank you for any help!