Question on deserializing byte[]

W

William

I have a strongly typed XML document created with xsd.exe. I have a web
request that returns an xml document as a byte[]. I would like to desearlize
the return byte array but not quite sure how. I am thinking of something
like:

TstRequest trqst= new TstFloodRequest();
CreateRequest(trqst); //populate the request document

WebClient wc = new WebClient();
MemoryStream ms = new MemoryStream();

XmlSerializer s1 = new XmlSerializer(typeof(TstRequest));
s1.Serialize(ms, trqst);
string req = Encoding.UTF8.GetString(ms.ToArray());

byte[] ret = ws.UploadData("http://testcompany.cgi", "POST",
Encoding.UTF8.GetBytes(req));

TstResponse trsp = new TstResponse ();
XmlSerializer s2 = new XmlSerializer(typeof(TstResponse));
s2.Deserialize(???); // I am trying to populate trsp with the returned
byte[] but not quite sure how to go about it.
 
M

Marc Gravell

Well, *if* (see below) the returned document *is* xml-serialized, then
the general approach would be something like:
byte[] data = {something}
XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
XmlDocument doc;
using (MemoryStream ms = new MemoryStream(data)) {
doc = (XmlDocument) ser.Deserialize(ms);
}

However when I read "returns an xml document as a byte[]", I think
first of just xml that happens to be represented as a byte[],
particularly as this byte[] is the result from a web-upload; perhaps
what you need is:
byte[] data = {something}
XmlDocument doc = new XmlDocument();
using (MemoryStream ms = new MemoryStream(data)) {
doc.Load(ms);
}
(or you can manually decode the byte[] to a string if you know the
encoding)

Marc
 

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

Top