Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
XmlSerializer
Message
From
31/05/2005 17:15:38
Keith Payne
Technical Marketing Solutions
Florida, United States
 
 
To
27/05/2005 18:51:25
General information
Forum:
ASP.NET
Category:
XML
Title:
Miscellaneous
Thread ID:
01018060
Message ID:
01018852
Views:
15
>Thanks mate. I wasn't aware of that one so I tried. However, there are still some problems, so I'll just post some code in here in hope that someone might shed some light on what I'm doing wrong.
>
>A bit info first: the code is from a function supposed to serialize an object, transform the XML using XSLT and finally send it as http post. The code should also receive an XML response from the remote service, transform it with XSLT and finally deserialize it back into the object.
>
>So here's the code, I'll comment on it as I go. Note that some of the code is there for testing purposes and is basically obsolete for the final version. Note also that I'm just a few weeks into C#, having coded VB since 98, prior to which I was sworn VFP man, thus ending up in this forum...
>
>System.Net.WebRequest req = System.Net.WebRequest.Create(RemoteServiceURL);
>req.Method = "POST";
>req.ContentType = "text/xml";
>// Get the request stream to write to
>System.IO.Stream streamWriter = (System.IO.Stream) req.GetRequestStream();
>
>System.Xml.Serialization.XmlSerializer xmltrans = new System.Xml.Serialization.XmlSerializer(typeof(PaymentContainer));
>
>// Create memory stream for Payment object serialization
>MemoryStream memStream = new MemoryStream();
>
>// Serialize using the memStream
>xmltrans.Serialize(memStream ,Payment);
>memStream.Seek(0, SeekOrigin.Begin);
>
>//Create XPath document for XML transformation
>XPathDocument myXPathDocument = new XPathDocument(memStream);
>XslTransform myXslTransform = new XslTransform();
>
>// Load XSLT for transformation
>string xsltDoc = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
>xsltDoc = xsltDoc.Replace("/","\\");
>xsltDoc += "PaymentSerialize.xsl";
>myXslTransform.Load(xsltDoc);
>
>// IDEALLY, AT THIS POINT THIS SHOULD DO THE TRICK
>// http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxslxsltransformclasstransformtopic16.asp
>// Call transform method and pass on the stream acquired from GetRequestStream()
>myXslTransform.Transform(myXPathDocument, null, streamWriter, null);
>
>//but this doesn't work (no error, though) so I'll wait with closing the stream
>//streamWriter.close();
>
>//test the serialization of object by serializing and writing it to disk
>string xsltTestDoc = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
>xsltTestDoc = xsltTestDoc.Replace("/","\\");
>xsltTestDoc += "PaymentSerializeTestDoc.xml";
>XmlTextWriter xwriter = new XmlTextWriter(xsltTestDoc , null);
>myXslTransform.Transform(myXPathDocument, null, xwriter, null);
>xwriter.Close();
>// Data in test XML document looks fine
>
>
>// Tried also with MemoryStream
>MemoryStream memStreamToSend = new MemoryStream();
>myXslTransform.Transform(myXPathDocument, null, memStreamToSend, null);
>memStreamToSend.Seek(0, SeekOrigin.Begin);
>
>// read the contents of the stream and write them manually to the http
>byte[] byteArray = new byte[memStreamToSend.Length];
>int count = memStreamToSend.Read(byteArray, 0, (int) memStreamToSend.Length);
>UnicodeEncoding uniEncoding = new UnicodeEncoding();
>char[] charArray = new char[uniEncoding.GetCharCount(byteArray, 0, count)];
>streamWriter.Write(byteArray, 0, byteArray.Length);
>
>// litte examining of the stream shows it has length=533 but all bytes are ''
>
>streamWriter.Close();
>System.Net.WebResponse response = req.GetResponse();
>
>StreamReader streamR = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
>string xmlString = streamR.ReadToEnd().Trim();
>
>// need to transform the XML response before deserializing
>
>PaymentContainer PaymentReceipt = (PaymentContainer) xmltrans.Deserialize(response.GetResponseStream());
>
Danijel,

Have you examined the contents of streamWriter after the call the Transform? The reason I ask is that you are declaring streamWriter as a base stream when you may have intended on declaring an actual streamwriter with the HTTPRequest.GetRequestStream as the base. Another possibility is that in a base stream, the Read, Write, and Seek methods may only be stubs. I couldn't say for sure because I haven't tried to use a base stream.

Other than that, I can't see anything wrong with the code from reading it.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform