Pass stream reader as paramaeter to webmethod

Posted on 2007-12-06
Last Modified: 2013-11-07
Hi all,

I have a web method that accepts a streamreader as parameter.I am trying to call that webmethod from webpage and pass astreamreader which reads an xml file to that webmethod.

But i am getting an error like value of type cannot be converted to type localhost.streamreader.

My requirement is client will send an xml file to my server application.I am reading that request using streamreader from a webpage in my server app.Then i am calling a webmethod which accepts parameter as stream reader to parse that xml file inside webmethod .

how can i send streamreader as parameter to that webmethod.

Open in new window

Question by:aditya_raj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
LVL 29

Expert Comment

by:Göran Andersson
ID: 20424675
You can't use a StreamReader as a parameter in a web method. It contains a live connection to the file that you are reading, and that can not be serialised into a string value to put in the SOAP message.

You have to send the data in a way that can be represented as a string, so just read the stream into a string, and send the string value to the web service.

Author Comment

ID: 20424728
I am not saving the xml file which i got from client.I am reading that request using

 Dim requestStream As Stream = Request.InputStream
 Dim rdr As StreamReader = New StreamReader(requestStream)

Can you please tell me without saving the file how can i make an xmlstring and pass it to webmethod.
LVL 29

Accepted Solution

Göran Andersson earned 500 total points
ID: 20424806
Like this:

Dim xml As String = rdr.ReadToEnd()

Also, you might need to specify the encoding from the request when you create the stream reader, to get it to read the stream correctly:

Dim rdr As StreamReader = New StreamReader(requestStream, Request.RequestEncoding)
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 20424830
<?xml version="1.0" standalone="yes"?>
Dim requestStream As Stream = Request.InputStream
 Dim rdr As StreamReader = New StreamReader(requestStream)
Dim requeststring As String = rdr.ReadToEnd()
I tried like that.
But i am getting Root element is missing error.
LVL 29

Expert Comment

by:Göran Andersson
ID: 20424853
That code only treats the data as a string, not an xml document, so it can't produce that error message.

What does the code look like where the error occurs?

Author Comment

ID: 20424875
Actually i want that reader to read using dataset and also need to convert xmlstring.But it is allowing to do only one function.

I will give my code.

Dim Requestds As New DataSet
        'reading the request object from client using the dataset
        Dim requestStream As Stream = Request.InputStream
        Dim rdr1 As StreamReader = New StreamReader(requestStream)
        Dim rdr2 As StreamReader = New StreamReader(requestStream)
        Dim xmlstring As String = rdr1.ReadToEnd

if i do like this it is going with no error.

But i can read the request stream only once.

Author Comment

ID: 20424904
please read the above comment too.
Can't i do more that one operation using that streamreader.
If i do rdr.readtoend  first i am getting then do dataset.readxml(rdr) i am getting root element missing.

In the same way,if i do dataset.readxml(rdr) first it is successful.Then on doing  rdr.readtoend is giving root element missing error

LVL 29

Expert Comment

by:Göran Andersson
ID: 20425799
First get the data into the string, then you can create a StringReader to read the string as a stream:

Dim xml As String = rdr.ReadToEnd()
Dim reader As New StringReader(xml)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question