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# / January 2008

Tip: Looking for answers? Try searching our database.

Proper Use of Streams

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fred Chateau - 08 Jan 2008 10:10 GMT
I need to send the following XML to a network connection and at the same
time, output it as a string to an ASPX page. How can I do this?

The network connection code works. If I try to read hmnStream, I get a
"Cannot Read Stream" exception.

HttpWebRequest hmnRequest = (HttpWebRequest)WebRequest.Create(wctXmlServer);
using (Stream hmnStream = hmnRequest.GetRequestStream())
{
 XmlTextWriter writer = new XmlTextWriter(hmnStream, Encoding.ASCII);
 writer.WriteStartElement("methodCall");
 writer.WriteElementString("methodName", "GetLogoFromLocationID");
 writer.WriteStartElement("params");
 writer.WriteStartElement("param");
 writer.WriteElementString("locationID", txtLocationID.Text.Trim());
 writer.WriteEndElement();
 writer.WriteEndElement();
 writer.WriteEndElement();
 writer.Close();

 StreamReader reqStream = new StreamReader(hmnStream); // Cannot Read
Stream Exception
 string output = reqStream.ReadToEnd();
 Response.Write(output);
}
ProcessRequest(hmnRequest);

Signature

Regards,

Fred Chateau
fchateauAtComcastDotNet

Jon Skeet [C# MVP] - 08 Jan 2008 10:16 GMT
> I need to send the following XML to a network connection and at the same
> time, output it as a string to an ASPX page. How can I do this?

GetRequestStream returns a stream into which you can *write* the data
you want as part of the *request*. It sounds to me like you actually
want the response, not the request.

Jon
Jon Skeet [C# MVP] - 08 Jan 2008 10:18 GMT
> I need to send the following XML to a network connection and at the same
> time, output it as a string to an ASPX page. How can I do this?

<snip>

Sorry, having reread the question:

1) Create an XmlDocument instead of an XmlWriter
2) Write the document to both the request stream and the ASP.NET page

Jon
Steven Cheng[MSFT] - 09 Jan 2008 02:57 GMT
Thanks for Jon's input.

Hi Fred,

As Jon suggested, you can use XmlDocument to construct the XML content so
as to reuse it for both network sending and ASPX rendering.

Also, if you want to reuse data in stream, I suggested you use the
"MemoryStream" class which can be randomly accessed and keep a copy of data
in memory. You can direcctly read/write data in bytes or wrapper it with
StreamReader/StreamWriter ....

#MemoryStream Class
http://msdn2.microsoft.com/en-us/library/system.io.memorystream(VS.71).aspx

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================
   

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
>From: "Jon Skeet [C# MVP]" <skeet@pobox.com>
>Newsgroups: microsoft.public.dotnet.languages.csharp
[quoted text clipped - 12 lines]
>
>Jon
Fred Chateau - 10 Jan 2008 09:55 GMT
Thanks for your help with this.

Signature

Regards,

Fred Chateau
fchateauAtComcastDotNet

> Thanks for Jon's input.
>
[quoted text clipped - 58 lines]
>>
>>Jon

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.