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

Process a List<Dictionary<int, T>> to a List<List<T>> C# generics [closed]
Possible to temporarily store a file locally before sending to ftp server?
WCF hybrid service
How to organize a HIERARCHY of non-UI threads and get status and progress back for UI consumption
Does the idea of 'Class' in C++/C#/Java come from struct in C? [duplicate]
Custom msmq trace listener
jQuery white label theme widget auto reload
mpi packet for c# paralleling program
The type of name space “” could not be found VS2013
C# Create a Truncated Collection with LINQ [duplicate]
How to get User Control in listbox data template item
Start from scratch MVC [closed]
using checkbox to enable textbox
Why are Anchor properties not being honored within dynamically created nested components?
Dataset data is different that stored procedure
Zip file being accessed by multiple user - Mem out of exception issue

Categories

HOME
netty
jenkins-pipeline
coordinates
powerquery
richtextbox
travis-ci-cli
sitemap
restheart
ephesoft
pyodbc
rename
email-attachments
jinja2
sha1
exchange-transport-agents
dat.gui
novacode-docx
legend-properties
bug-reporting
rndis
uipath
handle
android-xml
my.cnf
grunt-contrib-uglify
sphinx4
r-factor
ocl
paper-trail-gem
xlib
slim-lang
oracle-nosql
bootcamp
javax
sampling
cache-control
gstreamer-1.0
device-driver
latency
file-conversion
ninja-forms
mixed-models
vertical-alignment
glide-image-library
jvisualvm
collation
test-kitchen
qt-quick
risc
hdpi
riemann
android-vectordrawable
webmatrix
opticalflow
blackberry
rmq
stringbuilder
mac-app-store
settext
avi
user-profile
razorengine
pebble-js
treelist
paypal-subscriptions
oryx
angular-promise
play-json
windows-phone-8-emulator
traminer
autoscaling
firewire
grails-3.0.9
data-representation
bungeecord
esri-arc-engine
shunting-yard
famous-engine
amzi-prolog
episerver-7
pidgin
flask-mongoengine
stringstream
freeimage
hypertable
waveout
aspmenu-control
pinchzoom
yii-cactiverecord
soapexception
object-tag
hwnd
meego-harmattan
lwp
cpack
fxcopcmd
silverlight-2.0
j++
asp.net-1.1
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