Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9139785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:25:58+00:00 2026-06-17T09:25:58+00:00

I’m new to XML, since now I’ve only worked with Json:( I have a

  • 0

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#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:25:59+00:00Added an answer on June 17, 2026 at 9:25 am

    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 almost suit your needs):

    AdapterCards.XML

    <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>
    

    AdapterCards.XSL

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output indent="no" omit-xml-declaration="yes" method="text" encoding="UTF-8" media-type="text/x-json" />
    
    <xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
    <xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
    
        <!-- Main template for escaping strings; used by above template and for object-properties 
           Responsibilities: placed quotes around string, and chain up to next filter, escape-bs-string -->
        <xsl:template name="escape-string">
            <xsl:param name="s"/>
            <xsl:text>"</xsl:text>
            <xsl:call-template name="escape-bs-string">
                <xsl:with-param name="s" select="$s"/>
            </xsl:call-template>
            <xsl:text>"</xsl:text>
        </xsl:template>
    
        <!-- Escape the backslash (\) before everything else. -->
        <xsl:template name="escape-bs-string">
            <xsl:param name="s"/>
            <xsl:choose>
                <xsl:when test="contains($s,'\')">
                    <xsl:call-template name="escape-quot-string">
                        <xsl:with-param name="s" select="concat(substring-before($s,'\'),'\\')"/>
                    </xsl:call-template>
                    <xsl:call-template name="escape-bs-string">
                        <xsl:with-param name="s" select="substring-after($s,'\')"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="escape-quot-string">
                        <xsl:with-param name="s" select="$s"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <!-- Escape the double quote ("). -->
        <xsl:template name="escape-quot-string">
            <xsl:param name="s"/>
            <xsl:choose>
                <xsl:when test="contains($s,'&quot;')">
                    <xsl:call-template name="encode-string">
                        <xsl:with-param name="s" select="concat(substring-before($s,'&quot;'),'\&quot;')"/>
                    </xsl:call-template>
                    <xsl:call-template name="escape-quot-string">
                        <xsl:with-param name="s" select="substring-after($s,'&quot;')"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="encode-string">
                        <xsl:with-param name="s" select="$s"/>
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <!-- Replace tab, line feed and/or carriage return by its matching escape code. Can't escape backslash
           or double quote here, because they don't replace characters (&#x0; becomes \t), but they prefix 
           characters (\ becomes \\). Besides, backslash should be seperate anyway, because it should be 
           processed first. This function can't do that. -->
        <xsl:template name="encode-string">
            <xsl:param name="s"/>
            <xsl:choose>
                <!-- tab -->
                <xsl:when test="contains($s,'&#x9;')">
                    <xsl:call-template name="encode-string">
                        <xsl:with-param name="s" select="concat(substring-before($s,'&#x9;'),'\t',substring-after($s,'&#x9;'))"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- line feed -->
                <xsl:when test="contains($s,'&#xA;')">
                    <xsl:call-template name="encode-string">
                        <xsl:with-param name="s" select="concat(substring-before($s,'&#xA;'),'\n',substring-after($s,'&#xA;'))"/>
                    </xsl:call-template>
                </xsl:when>
                <!-- carriage return -->
                <xsl:when test="contains($s,'&#xD;')">
                    <xsl:call-template name="encode-string">
                        <xsl:with-param name="s" select="concat(substring-before($s,'&#xD;'),'\r',substring-after($s,'&#xD;'))"/>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$s"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
    
        <xsl:template match="card">
            <xsl:text>{</xsl:text>
    
            <xsl:text>"type":</xsl:text>
            <xsl:call-template name="escape-string">
                <xsl:with-param name="s" select="translate(../@type, $uppercase, $smallcase)"/>
            </xsl:call-template>
    
            <xsl:text>,"id":</xsl:text>
            <xsl:call-template name="escape-string">
                <xsl:with-param name="s" select="id"/>
            </xsl:call-template>
    
            <xsl:text>,"description":</xsl:text>
            <xsl:call-template name="escape-string">
                <xsl:with-param name="s" select="description"/>
            </xsl:call-template>
    
            <xsl:text>,"mccode":</xsl:text>
            <xsl:call-template name="escape-string">
                <xsl:with-param name="s" select="mccode"/>
            </xsl:call-template>
    
            <xsl:if test="following::card">},</xsl:if>
            <xsl:if test="not(following::card)">}</xsl:if>
        </xsl:template>
    
    
        <xsl:template match="/AdapterCards">
            <xsl:text>{[</xsl:text>
    
            <xsl:apply-templates select="cards/card" />
    
            <xsl:text>]}</xsl:text>
      </xsl:template>
    </xsl:stylesheet>
    

    C#

    // Load XML document
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load("AdapterCards.XML");
    
    // Transform the XML into JSON
    XslCompiledTransform transformer = new XslCompiledTransform();
    using (var xslStylesheetFile = File.Open("AdapterCards.XSL", FileMode.Open))
    {
        using (var xmlReader = new XmlTextReader(xslStylesheetFile))
        {
            transformer.Load(xmlReader);
        }
    }
    var sourceNavigator = xmlDoc.CreateNavigator();
    using (MemoryStream ms = new MemoryStream())
    {
        transformer.Transform(sourceNavigator, null, ms);
        ms.Position = 0;
        using (var sr = new StreamReader(ms))
        {
            return sr.ReadToEnd(); // <-- this is your JSON
        }
    }
    

    I executed the above XSL in Notepad++ and got the following:

    {[{"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"}]}
    

    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 function fn:lower-case().

    EDIT3: Done – case is now translated to lower with the help of this answer.

    References:

    • xml2json-xslt
    • How can I convert a string to upper- or lower-case with XSLT?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I would like to run a str_replace or preg_replace which looks for certain words
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.