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

Uploading a file via FTP
Why will the addressing method that works for images not work for text files?
Previously tested Regex is not working now, but works in Regex Tester
Dependency property never being set
How to keep the original document when projecting in mongodb?
Use split to remove parts of a string that are surrounded by curly double quotes
Installing json.net via NU get error 'Newtonsoft.Json' already has a dependency defined for Microsoft C#
Only insert data into a database if it doesn't exist already
Entity Framework not Finding Table
Windows Service Communicate WIth UI
How can I prevent screen recording using C#?
Angular $http.post() to C# Web API Controller
Return parameter in controller instead a View
How can I pause the audio source and animation at the same time in unity 3d
When should I use ServiceFabricIntegrationOptions.UseUniqueServiceUrl
Regex match and replace operators in math operation

Categories

HOME
google-cloud-pubsub
opencv4android
magento-1.9.2.1
pip
sitemap
runtime
olap
google-search-appliance
unreal-engine4
camunda
logstash-configuration
luis
nsoperation
amazon-data-pipeline
powerpivot
vivado-hls
rndis
beaker-notebook
email-client
datanucleus
multiple-inheritance
setup-project
javapns
edirectory
jboss-arquillian
opencsv
jql
keil
wercker
favorites
epub
ansible-inventory
flex3
javafxports
byobu
linkurious
cowboy
office-interop
sknode
google-cloud-powershell
rexx
synchronized
text-formatting
confluent
px4
jcifs
lattice
android-music-player
cedar-bdd
diameter-protocol
transaction-isolation
user-profile
pebble-js
calc
video-embedding
firefox-os
yfiles
ruby-1.8.7
event-tracking
jts
self-join
updating
windows-phone-8-emulator
joomla3.3
eula
parseexception
pymol
numerical-recipes
fedena
grunt-contrib-connect
gnip
tigase
time-frequency
triplestore
reactjs-native
hotlinking
yandex-api
traceur
iwork
kuka-krl
.net-reflector
ie-developer-tools
violation
listpicker
wordbreaker
cocotron
coinbase-php
junit-rule
jquery-1.8
sup
reflexil
advanceddatagrid
parallel-python
lwp
hs-err
for-xml
mysql-pconnect
djangoappengine
user-preferences
software-update

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