Convert Xml list to Json

Feb 28, 2012 at 12:51 PM

Hi

I have a list that have been generated by a XmlSerializer. The result look like:

<?xml version="1.0" encoding="utf-8"?>
<MyObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <UsernameHistory>
    <string>admin</string>
    <string>John</string>
  </DatastoreHistory>
</MyObject>

After, I try to use this code snippet:

string jsonText = JsonConvert.SerializeXmlNode(doc, Formatting.None, true);

var obj = JsonConvert.DeserializeObject
(
    jsonText,
    typeof(MyObject)
);

Than, I got an error. Two things, Json.Net doesn't seems to like when there is namespace in the xml. That I can easily remove it.
The things I don't know how to handle is that Json.Net seems to be only able to manage xml list like:

<MyObject>
  <UsernameHistory>admin</UsernameHistory>
  <UsernameHistory>john</UsernameHistory>
</MyObject>

Is there a way than Json.Net can handle easily the list serialized by a XmlSerializer?

thx