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)
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

622 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