0

I have an XML and when converting to JSON, all the values became string as below:

XML: <root><deviceToEgressLocationDistance>600</deviceToEgressLocationDistance></root> 
JSON: { "deviceToEgressLocationDistance": "600" } 

while it should be

JSON: { "deviceToEgressLocationDistance": 600 } 

below the code I am using but it converts everything to strings

XmlDocument doc = new XmlDocument(); doc.LoadXml(xmlString); string jsonContent = JsonConvert.SerializeXmlNode(doc, Formatting.Indented, true); 

I have checked the solution in Serialize Xml to Json using type attribute in c#

but I cannot manually parse every value as the xml file properties are changing every time so the next time it could be

XML: <root><Location1>500.88</Location1><Location2>650</Location2></root> 

The xml could have any structure including nesting properties.

Any ideas?

Update: After converting the XML to JSON, this JSON file is going to be the source to another system and if the JSON file has the wrong types like treating numbers as string or even DateTime as string, the subsystem fail to process the input source. So, I need to preserve the types in XML when converting to JSON

6
  • 4
    Deserialize this into an object model which has int deviceToEgressLocationDistance; and the re-serialize as JSON Commented Aug 11, 2022 at 20:33
  • Does this answer your question? How to convert JSON to XML or XML to JSON? Commented Aug 11, 2022 at 23:00
  • No, They are using the exact code in my question. it does not solve the type issue Commented Aug 12, 2022 at 0:18
  • You have to do it like in this post. First, convert XML to Object. You need to define class for this object. Later, convert object to JSON. Commented Aug 12, 2022 at 8:15
  • @ahmetgül as I mentioned in my question, I do not have a schema. the file structure is changing so I cannot really convert to an object since I won't have a class to convert to. Commented Aug 12, 2022 at 16:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.