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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:01:50+00:00 2026-06-02T16:01:50+00:00

Is JavascriptSerializer the tool to convert an xml file (of unknown schema) into a

  • 0

Is JavascriptSerializer the “tool” to convert an xml file (of unknown schema) into a json string ?

There are some threads here dealing about how to convert xml to json in c#.
And some recommended dedicated solutions (http://www.phdcc.com/xml2json.htm)

But in those threads there are always one suggesting using JavaScriptSerializer. But there is never clear explanation on how to do it. One always elude it or start with an object instead of an xml.

To make it clear :
I don’t look after having my xml turned into objects.
If I can, I’d prefer to avoid it.
XML => Json would please me more than XML => objects => Json.

But everybody is telling don’t reinvent wheel use JavaScriptSerializer. But I don’t feel like this is the way to go. Setting up objects from xml looks like a terrible task (strongly typing).

So my question is :

Should I stay with the quick (but “dirty”) methods described in http://www.phdcc.com/xml2json.htm

Or

Could I use JavascriptSerializer even if I don’t know the schema of the xml ?
If so please fill in the gaps/modify the following code

namespace ExtensionMethods {  
    public static class JSONHelper     
    {
        public static string ToJSON(this XmlDocument doc)
        {  
            object obj = get_An_Object_From_My_XML_Without_Too_Much_Hassle_Like_Having_To_Deal_With_Strongly_Type(doc); // how to do that ???            
            JavaScriptSerializer serializer = new JavaScriptSerializer(); 
            return serializer.Serialize(obj);
        }    
    }   
}

using ExtensionMethods; 
...
XmlDocument mydoc = new XmlDocument(@"c:\test.xml");
Response.write(mydoc.ToJSON());
  • 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-02T16:01:50+00:00Added an answer on June 2, 2026 at 4:01 pm

    I think you could use json.net in order to receive a unknown xml into a json object:

    string xml = @"<?xml version=""1.0"" standalone=""no""?>
    <root>
      <person id=""1"">
      <name>Alan</name>
      <url>http://www.google.com</url>
      </person>
      <person id=""2"">
      <name>Louis</name>
      <url>http://www.yahoo.com</url>
      </person>
    </root>";
    
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    
    string jsonText = JsonConvert.SerializeXmlNode(doc);
    //{
    //  "?xml": {
    //    "@version": "1.0",
    //    "@standalone": "no"
    //  },
    //  "root": {
    //    "person": [
    //      {
    //        "@id": "1",
    //        "name": "Alan",
    //        "url": "http://www.google.com"
    //      },
    //      {
    //        "@id": "2",
    //        "name": "Louis",
    //        "url": "http://www.yahoo.com"
    //      }
    //    ]
    //  }
    //}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble serializing an object to a JSON string using System.Web.Script.Serialization.JavaScriptSerializer. Whenever
My C# web method uses JavaScriptSerializer to return a JSON object in string format
Hi Im having trouble getting the Javascript serialiser to convert this Json string to
To start, I know there are two kinds of JSON serialization currently built into
I'm using .NET's JavascriptSerializer to deserialize JSON into runtime objects and for the most
here is the basic code: public static string DeserializeNames() { jsonData = {\name\:[{\last\:\Smith\},{\last\:\Doe\}]}; JavaScriptSerializer
I tried: NameValueCollection Data = new NameValueCollection(); Data.Add(foo,baa); string json = new JavaScriptSerializer().Serialize(Data); it
Im using the JavaScriptSerializer Class to serilize and Deserilize in/to Json. I know there
I have some JSON that I need to deserialize so I'm using JavaScriptSerializer.DeserializeObject like:
I have some C# web services that return JSON. The .NET JavaScriptSerializer returns dates

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.