c#


Send XML file on request


I'm trying to send a XML file on request, but I'm getting an error when I'm trying to copy the stream, which I'm loading the file into, to the output stream.
Right now it's working fine if I'm making the request (I use HttpListener btw) from a browser; it shows me my .xml just fine. But I'd also like to be able to download the .xml when I make the request.
Any suggestions?
string xString = #"C:\Src\Capabilities.xml";
XDocument capabilities = XDocument.Load(xString);
Stream stream = response.OutputStream;
response.ContentType = "text/xml";
capabilities.Save(stream);
CopyStream(stream, response.OutputStream);
stream.Close();
public static void CopyStream(Stream input, Stream output)
{
input.CopyTo(output);
}
The error I'm getting is at input.CopyTo(output); : "Stream does not support reading."
You probably get the error because the stream input actually is the response.OutputStream, which is an output stream and also makes the source and target of the copy operation the same stream - huh?
Essentially what your code does now (and this is wrong): You save the XML content to the response's output stream (which essentially already sends it to the browser). Then you try to copy the output stream into the output stream. This doesn't work and even if it did - why? You already wrote to the output stream.
You can simplify all this greatly in my opinion as follows:
// Read the XML text into a variable - why use XDocument at all?
string xString = #"C:\Src\Capabilities.xml";
string xmlText = File.ReadAllText(xString);
// Create an UTF8 byte buffer from it (assuming UTF8 is the desired encoding)
byte[] xmlBuffer = Encoding.UTF8.GetBytes(xmlText);
// Write the UTF8 byte buffer to the response stream
Stream stream = response.OutputStream;
response.ContentType = "text/xml";
response.ContentEncoding = Encoding.UTF8;
stream.Write(xmlBuffer, 0, xmlBuffer.Length);
// Done
stream.Close();

Related Links

Converting some C# code to managed or C++/CLI code
DataTemplate for DataGrid
Error for storing paragraph data in MYSQL
Getting last transaction from a table using LINQ
Submitting a form data in to grid view
How to take elements part successive from list in C#?
Having trouble with monodevelop
Access to the path Server.MapPath is denied
Give `string[] args` to Linqpad script
c# SoundPlayer playlist loop
how to customize sync according to selected data only sync framework / trick to do it
C# Winforms control - DataGridView - Update Database
What is the purpose of specifying from attribute in smtp element?
Detect no navigation in windows 8 metro
Set header in Excel for worksheet 2
Winforms, Scintilla - Saving shortcut inserts 's' letter to editor

Categories

HOME
model-view-controller
grizzly
azure-active-directory
display
android-ndk
blob
latex
ebay-api
css-selectors
ms-access-2010
uml-designer
browserstack
sahi
core-data
unreal-engine4
captcha
mbed
fastreport
saucelabs
eclipse-orion
sha1
appsettings
brightway
simd
jquery-easyui
airbnb
indri
android-wifi
inline-assembly
email-client
my.cnf
dynamics-ax-2012-r3
edirectory
choco
visual-prolog
scene7
availability
offset
leaderboard
rythm
mongoid5
motion
instances
idea-plugin
susy
aura-framework
firefox-developer-edition
niagara-ax
opos
noise
dcom
visualizer
wink
programming-pearls
acceleo
breadcrumbs
android-tabs
foreground
magick.net
shtml
laterjs
salesforce-service-cloud
periodic-processing
wizard
wordpress-plugin
jquery-draggable
dynamics-nav-2013
webservices-client
javapackager
uinavigationitem
cloud-storage
openhab
consul-template
nfa
cl.exe
teamstudio-unplugged
method-overloading
fscanf
time-frequency
isl
visual-studio-setup-proje
node-mongodb-native
starling-framework
kuka-krl
xtify
maven-javadoc-plugin
coinbase-php
rbm
urlfetch
idoc
waveout
nsscrollview
datejs
jqzoom
clgeocoder
mcpd
mscorlib
httppostedfilebase
overlays
norton
netbeans7.0
lgpl
activescaffold
versions
html-help-workshop
tracd

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App