Proper Use of Streams

F

Fred Chateau

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

Jon Skeet [C# MVP]

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
 
J

Jon Skeet [C# MVP]

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
 
S

Steven Cheng[MSFT]

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]" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: Re: Proper Use of Streams
Date: Tue, 8 Jan 2008 02:18:12 -0800 (PST)

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
 
F

Fred Chateau

Thanks for your help with this.

--
Regards,

Fred Chateau
fchateauAtComcastDotNet

Steven Cheng said:
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]" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Subject: Re: Proper Use of Streams
Date: Tue, 8 Jan 2008 02:18:12 -0800 (PST)

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
 

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