Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

Question on deserializing byte[]

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
William - 23 Aug 2006 22:08 GMT
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.
Marc Gravell - 24 Aug 2006 05:59 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.