title: (don't ?) use JavaScriptSerializer to convert xml file (of unknown schema) to json in c#
body: <p>Is JavascriptSerializer the "tool" to convert an xml file (of unknown schema) into a json string ?</p>
  
  <p>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)</p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>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).</p>
  
  <p>So my question is :</p>
  
  <p>Should I stay with the quick (but "dirty") methods described in <a href="http://www.phdcc.com/xml2json.htm" rel="nofollow">http://www.phdcc.com/xml2json.htm</a></p>
  
  <p>Or</p>
  
  <p>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</p>
  
  <pre><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());
  </code></pre>