C# Object to/from JSON / XML possible?!?

Last post 01-17-2007 3:47 PM by Steve Marx. 3 replies.

Sort Posts:

  • C# Object to/from JSON / XML possible?!?

    01-17-2007, 8:31 AM
    • Member
      275 point Member
    • quantass0
    • Member since 03-30-2006, 6:20 PM
    • Posts 117

    Im using ASP.NET C# (framework v1.1) under Windows 2003.  I'm trying to serialize my class object to return as JSON to my awaiting Javascript code, then take in JSON from the client side and convert it back to my C# object.  I've tried using XmlSerializer, AjaxPro .Net, and some misc code but everything has problems; XmlSerializer refuses to serialize my class because it contains an ArrayList that stores a different class type:

    public class MyClass
    {
        int a=1;
        ArrayList b = new ArrayList();

    XmlDocument xml;
    }

    public class MySection
    {
       int x=1;
    }

    MyClass mClass = new MyClass();
    mClass.b.Add(new MySection());

    XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
    StringWriter sw = new StringWriter();
    serializer.Serialize(sw, mClass);
    sw.Close();

    Eventually when i try to serialize mClass to a string I receive a useless error:
    An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll
    Additional information: There was an error generating the XML document.

    AjaxPro.NET has a nice Serializer to string but it is completely clueless when it comes to XmlDocument, a member of my class.  

    One person has recommended Ajax.NET but I havent read up on it and dont want to go down yet another dead end.  Is there a library for C# that can fully convert a class object (regeardless of member type and values) to JSON or XML and can also go in reverse to produce a proper C# object from XML/JSON?  I'm also open to a client-side solution.  If Ajax.NET is the solution does it provide the functionality for .NET Framework v1.1 C#?

    My class data types can be: intrinsic types, ArrayLists, objects derived from other classes, XmlDocument.

    Thanks.

  • Re: C# Object to/from JSON / XML possible?!?

    01-17-2007, 10:09 AM
    • All-Star
      25,662 point All-Star
    • Luis Abreu
    • Member since 02-12-2005, 6:22 AM
    • Madeira [Portugal]
    • Posts 5,368
    • TrustedFriends-MVPs

    hello.

    if you don't have circular references in your classes, then the default json serializers added by ajax extensions are more than enough...

    --
    Regards,
    Luis Abreu
    email: labreu_at_gmail.com
    EN blog:http://msmvps.com/blogs/luisabreu
  • Re: C# Object to/from JSON / XML possible?!?

    01-17-2007, 3:37 PM
    • Member
      275 point Member
    • quantass0
    • Member since 03-30-2006, 6:20 PM
    • Posts 117

    Is this Ajax extension for Asp.Net v1.1?  Where can i find it?

     

    Thanks

  • Re: C# Object to/from JSON / XML possible?!?

    01-17-2007, 3:47 PM
    • Contributor
      2,460 point Contributor
    • Steve Marx
    • Member since 05-26-2006, 4:35 PM
    • Microsoft
    • Posts 643
    ASP.NET AJAX (http://ajax.asp.net) is only for ASP.NET 2.0.
    Steve Marx | ASP.NET AJAX Evangelist | Microsoft Corporation
Page 1 of 1 (4 items)