I have a piece of code that generate an XML base of a "template" file, all the data is dynamically taken from the project, base on current user..
And what i need to do, is to send JSON structure string to an API (i have no control on).
The problem which i am facing is that i can't manage to generate such a JSON(in the right format) from the XML using JsonConvert.SerializeObject
.
With the help of an online tool i have creates this XML from that JSON, JsonConvert.SerializeObject(XML)
generates a JSON, but instead, for example element
that represent an array item - i need each item to be in []
, RootDTO
the root element, i don't need it at all.
So what i need to accomplish is turning this XML to a JSON that structured like that JSON.
Is it possible with the use of "json.net"? Do i need to write a "custom serilizer"
The XML i have:
<?xml version="1.0" encoding="UTF-8"?>
<RootDTO>
<destination>
<name>companyName</name>
</destination>
<orderData>
<amount>123.45</amount>
<items>
<element>
<binding>saddle</binding>
<components>
<element>
<width>210</width>
</element>
</components>
<description>Introductory x</description>
</element>
</items>
</orderData>
</RootDTO>
The JSON JsonConvert.SerializeObject
produce
{
"?xml": {
"@version": "1.0",
"@encoding": "UTF-8"
},
"RootDTO": {
"destination": {
"name": "companyName"
},
"orderData": {
"amount": "123.45",
"items": {
"element": {
"binding": "saddle",
"components": {
"element": {
"width": "210"
}
},
"description": "Introductory x"
}
}
}
}
}
The desired JSON
{
"destination": {
"name": "companyName"
},
"orderData": {
"amount": "123.45",
"items": [
{
"binding": "saddle",
"components": [
{
"width": "210"
}
],
"description": "Introductory x"
}
]
}
}