Solved

Pass stream reader as paramaeter to webmethod asp.net vb.net

Posted on 2007-12-06
8
1,674 Views
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 system.io.streamreader 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

0
Comment
Question by:aditya_raj
  • 4
  • 4
8 Comments
 
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.
0
 

Author Comment

by:aditya_raj
ID: 20424728
OK.
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.
0
 
LVL 29

Accepted Solution

by:
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)
0
 

Author Comment

by:aditya_raj
ID: 20424830
<?xml version="1.0" standalone="yes"?>
 <Details>
            <UserName>piscesIt</UserName>
            <Password>hello</Password>
            <CustomerNumber>7323</CustomerNumber>
            <NodeAccountCode>Pisces15</NodeAccountCode>
            <AccountPassword>hello</AccountPassword>
</Details>
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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?
0
 

Author Comment

by:aditya_raj
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
        Requestds.ReadXml(rdr2)

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

But i can read the request stream only once.
0
 

Author Comment

by:aditya_raj
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 succesful.an 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


0
 
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()
rdr.Close()
Dim reader As New StringReader(xml)
dataset.ReadXml(reader)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now