transferring state in a XML serialized object

J

Jack Fox

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);
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top