I'm new to XML, since now I've only worked with Json:( I have a XML file which looks like this:
<AdapterCards>
<cards type="MCS">
<card>
<id>id1</id>
<description>desc1</description>
<mccode>code1</mccode>
</card>
<card>
<id>id2</id>
<description>desc2</description>
<mccode>code2</mccode>
</card>
</cards>
<cards type="MCM">
<card>
<id>id3</id>
<description>desc3</description>
<mccode>code3</mccode>
</card>
<card>
<id>id4</id>
<description>desc4</description>
<mccode>code4</mccode>
</card>
</cards>
<cards type="F"/>
<cards type="B"/>
</AdapterCards>
I want to parse it to a json string which should look like this:
{[{'type': 'mcs', 'id': 'id1', 'description': 'desc1', 'mccode': 'code1'},
{'type': 'mcs', 'id': 'id2', 'description': 'desc2', 'mccode': 'code2'},
{'type': 'mcm', 'id': 'id3', 'description': 'desc3', 'mccode': 'code3'},
{'type': 'mcm', 'id': 'id4', 'description': 'desc4', 'mccode': 'code4'}
]}
My problem is that I haven't worked with XML at all (yep, shame on me). Could you please give me some leads on how to parse the xml in a fast way (I have it on a Stream, I uploaded it on server). I have searched for some XML to Json converters, but it's impossible to fine one to suit my needs, since I need a "special" format.
Thank you for the answers:)! I'm using C#.
I had to write a bespoke solution for the same type of thing myself recently. I did it with XSLT, using the
XslCompiledTransform
class to run an input of XML and output JSON.This needs some work but should help you with the basics (it is copy-pasted from the work I did, changed to
almostsuit your needs):AdapterCards.XML
AdapterCards.XSL
C#
I executed the above XSL in Notepad++ and got the following:
As you can see, there's commas missing where they need to be. But it's almost there!!Updated the XSL and the output which now work with commas in the right place. Next thing to fix is the case of "type". I'm thinking you might need to use XSL 2.0 to gain access to the xpath functionfn:lower-case()
.EDIT3: Done - case is now translated to lower with the help of this answer.
References: