transferring state in a XML serialized object

Discussion in 'ASP .Net' started by Jack Fox, Dec 2, 2003.

  1. Jack Fox

    Jack Fox Guest

    Some dotnet functionality only works in conjunction with streams. A couple
    of times I have only wanted to place the results in a string. This is one of
    those cases. Is there any way I can do this in fewer lines of code?


    It was suggested to me to pass state from one page to the next by
    serializing an object. I was further told that it is a one line process on
    each end to serialize a simple object to XML and transfer it to another
    page, but I can't figure out how to do it in fewer than 9 lines on each end.
    (I suppose I could combine the last 3 lines on the sending side into one
    huge line.)


    On the sending page:


    clsSimple oSimple = new clsSimple("MyName", "MyType"); //constructor inits
    2 properties in simple class

    XmlSerializer ser = new XmlSerializer(typeof(clsSimple));

    MemoryStream ms = new MemoryStream();
    ser.Serialize(ms, oSimple);

    Encoding ascii = Encoding.ASCII;
    int k = ms.GetBuffer().Length;
    char[] asciiChars = new char[k];
    ascii.GetChars(ms.GetBuffer(), 0, k, asciiChars, 0);

    Server.Transfer("WebForm2.aspx?myclass=" + HttpUtility.UrlEncode(new
    string(asciiChars)));



    On the receiving page:

    MemoryStream ms = new MemoryStream();
    XmlSerializer ser = new XmlSerializer(typeof(clsSimple));

    char[] asciiChars = oQuery.Get(0).ToCharArray();
    byte[] MyBytes = new byte[asciiChars.Length];

    for (int i = 0; i < MyBytes.Length; i++)
    MyBytes = Convert.ToByte(asciiChars);

    ms.Write(MyBytes, 0, MyBytes.Length);
    ms.Position = 0;

    clsSimple oSimple = (clsSimple)ser.Deserialize(ms);
    Jack Fox, Dec 2, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcel Balcarek

    storing serialized object in DB problem

    Marcel Balcarek, Apr 15, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    319
    Steve C. Orr [MVP, MCSD]
    Apr 15, 2004
  2. madani
    Replies:
    0
    Views:
    452
    madani
    Mar 4, 2006
  3. Francois Gagnon

    Problem with URL encode serialized object

    Francois Gagnon, Mar 2, 2004, in forum: Java
    Replies:
    1
    Views:
    814
    John C. Bollinger
    Mar 2, 2004
  4. Smokey Grindel
    Replies:
    2
    Views:
    300
    Smokey Grindel
    Dec 30, 2006
  5. Don Miller
    Replies:
    4
    Views:
    590
    Don Miller
    Feb 6, 2007
Loading...

Share This Page