I need to convert a large number of XML files into JSON. The XML files all have same structure, and differs only in data that they contain.

I could write a basic C# utility that would parse XML and then create JSON file. However, since all JSONs would be same in structure (since all XML are same), would it be better to use T4 Template for this conversion?

Should T4 Templates be used for such purposes? Or any other recommendations?

  • You could use the Paste XML as Classes feature in Visual studio to quickly generate some DTOs based on your XML data (using XmlSerializer to deserialise the XML data itself), then serialise those DTOs using Newtonsoft.Json. – Ben Cottrell Oct 12 '17 at 12:37

A code transformation utility sounds more appropriate than T4 for this form of transformation.

T4 is convenient when you have to generate a lot of boilerplate, and it is easier to write this boilerplate as literal text in a template rather than generate it by jugglings strings or building some model. So it is nice for data-driven code generation. But in converting a data structure from XML to JSON there is (probably) not any boilerplate. There already exist JSON serializes which can generate valid JSON output, so the challenge is only in mapping the one format to anther.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.