3

I am trying to convert JSON to XML using newtonsoft, but because the left part contains a number and XML cannot have element that starts with a number the conversion fails

Example:

{
   "PLC": {
       "10": 7.6,
       "9": 1.8,
       "4": 11
      }
}

is there anyway to safely convert it? or make it to add a "_" prefix for example to elements that starts with a number?

3
  • What are you using this for? Commented Sep 29, 2014 at 14:43
  • It is likely you just need to follow guidance in the documentation: Converting between JSON and XML "If the XML created from JSON doesn't match what you want then you will need to convert it manually.". Consider to add more information to your post about what XML do you want as result or why you even trying to do that conversion. Commented Sep 29, 2014 at 14:45
  • an example would be <PLC> <_10>7.6</_10> <_9>1.8</_9> <_4>11</_4> </PLC> i need it so i can work with the document as XElement Commented Sep 29, 2014 at 15:00

1 Answer 1

1

Obviously there is no general way to safely convert it. XML element and attribute names cannot start with a digit. If you use tricks such as prepending _ to the element name or introducing special elements in a special namespace (e.g. <atikot:item id="10">7.6</atikot:item>) to express this construct, you lose generality and you will need special processing on back-conversion, XML serialization and deserialization too. You may also have problems with DTDs if you use any. You have to ask yourself first what sort of XML do you need to obtain after the conversion and what it will be used for.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.