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 8420653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:55:38+00:00 2026-06-10T02:55:38+00:00

I have the following XML file that I need to convert to JSON in

  • 0

I have the following XML file that I need to convert to JSON in the server. Initially I thought I would convert it to a Dictionary and then use the JavaScriptSerializer to turn it into JSON but since each column could have a different value type, I don’t think it would work. Has anyone done something similar before in C#/LINQ?

I need to preserve the Value Types(Boolean, String, Integer) of each column.

I would appreciate any advice on this as Im just starting to work with XML. Thanks.

<Columns>
 <Column Name="key1" DataType="Boolean">True</Column>
 <Column Name="key2" DataType="String">Hello World</Column>
 <Column Name="key3" DataType="Integer">999</Column>
</Columns>
  • 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-10T02:55:39+00:00Added an answer on June 10, 2026 at 2:55 am
    using System;
    using System.Linq;
    using System.Web.Script.Serialization;
    using System.Xml.Linq;
    
    class Program
    {
        static void Main()
        {
            var xml = 
            @"<Columns>
              <Column Name=""key1"" DataType=""Boolean"">True</Column>
              <Column Name=""key2"" DataType=""String"">Hello World</Column>
              <Column Name=""key3"" DataType=""Integer"">999</Column>
            </Columns>";
            var dic = XDocument
                .Parse(xml)
                .Descendants("Column")
                .ToDictionary(
                    c => c.Attribute("Name").Value, 
                    c => c.Value
                );
            var json = new JavaScriptSerializer().Serialize(dic);
            Console.WriteLine(json);
        }
    }
    

    produces:

    {"key1":"True","key2":"Hello World","key3":"999"}
    

    Obviously this treats all the values as strings. If you want to keep the underlying type semantics you could do the following:

    using System;
    using System.Linq;
    using System.Web.Script.Serialization;
    using System.Xml.Linq;
    
    class Program
    {
        static void Main()
        {
            var xml = 
            @"<Columns>
              <Column Name=""key1"" DataType=""System.Boolean"">True</Column>
              <Column Name=""key2"" DataType=""System.String"">Hello World</Column>
              <Column Name=""key3"" DataType=""System.Int32"">999</Column>
            </Columns>";
            var dic = XDocument
                .Parse(xml)
                .Descendants("Column")
                .ToDictionary(
                    c => c.Attribute("Name").Value, 
                    c => Convert.ChangeType(
                        c.Value,
                        typeof(string).Assembly.GetType(c.Attribute("DataType").Value, true)
                    )
                );
            var json = new JavaScriptSerializer().Serialize(dic);
            Console.WriteLine(json);
        }
    }
    

    produces:

    {"key1":true,"key2":"Hello World","key3":999}
    

    And if you cannot modify the underlying XML structure you will need a custom function that will convert between your custom types and the underlying .NET type:

    using System;
    using System.Linq;
    using System.Web.Script.Serialization;
    using System.Xml.Linq;
    
    class Program
    {
        static void Main()
        {
            var xml = 
            @"<Columns>
              <Column Name=""key1"" DataType=""Boolean"">True</Column>
              <Column Name=""key2"" DataType=""String"">Hello World</Column>
              <Column Name=""key3"" DataType=""Integer"">999</Column>
            </Columns>";
            var dic = XDocument
                .Parse(xml)
                .Descendants("Column")
                .ToDictionary(
                    c => c.Attribute("Name").Value, 
                    c => Convert.ChangeType(
                        c.Value, 
                        GetType(c.Attribute("DataType").Value)
                    )
                );
            var json = new JavaScriptSerializer().Serialize(dic);
            Console.WriteLine(json);
        }
    
        private static Type GetType(string type)
        {
            switch (type)
            {
                case "Integer":
                    return typeof(int);
                case "String":
                    return typeof(string);
                case "Boolean":
                    return typeof(bool);
                // TODO: add any other types that you want to support
                default:
                    throw new NotSupportedException(
                        string.Format("The type {0} is not supported", type)
                    );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following xml file that I need to read into my C#
We have a project where we need to convert the following file types to
i have the following question: I have a XML file with some elements that
I have an XML file that I need to apply a namespace to at
For example I have file config.xml in that file I have following xml content
I have an xml file that I am parsing and I have the following
I have following xml file: <ab> <![CDATA[ <table> <tbody> <tr> <th>abcdef</th> </tr> <tr> <p>
I have the following XML File: <persons> <person name=shawn> <age>34</age> <hair style=spikes>red</hair> </person> <person
I have the following XML file:- <?xml version=1.0 encoding=UTF-8?> <viewentries> <viewentry position=1> <entrydata columnnumber=0>
I have the following xml file: <xfa:data> <form1> <Page1> <Page2> <contractInfo> ... </contractInfo> <paymentInfo>

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.