Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Pass stream reader as paramaeter to webmethod

Posted on 2007-12-06
Medium Priority
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 2000 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)
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!


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

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

722 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