How to return XML/XmlDocument from a web service?

Q

Quick

Ok, please don't hold this against me, I know there are like 50 topics
out there about this, but none of them really address the basics of
this issue and I'm hoping there's someone around that can help.

I'm looking for help implementing a very basic example.

I have a web service with one web method that returns a DataSet that
contains 1 DataTable.

I want to pass back an XML representation of the DataSet or DataTable
(don't care which) to a .Net web app that's going to consume the web
service.

Once the XML gets passed to the client application i want to take it
and put it back into a DataTable.

Is there any reasonably straight forward way to accomplish this and if
so can someone give me an example?

For some reason I had the impression that you could move data between
DataSets as strings of XML using the GetXML() method and some other
method to get it back in, but I can't see how to pass the string
generated by the GetXML() method into some other DataSet object. I
know there's a ReadXML() method, but I can't figure that out at the
moment. I'd appreciate any help I could get on this.

Thanks,

-Quick
 
Q

Quick

Ok, please don't hold this against me, I know there are like 50 topics
out there about this, but none of them really address the basics of
this issue and I'm hoping there's someone around that can help.

I'm looking for help implementing a very basic example.

I have a web service with one web method that returns a DataSet that
contains 1 DataTable.

I want to pass back an XML representation of the DataSet or DataTable
(don't care which) to a .Net web app that's going to consume the web
service.

Once the XML gets passed to the client application i want to take it
and put it back into a DataTable.

Is there any reasonably straight forward way to accomplish this and if
so can someone give me an example?

For some reason I had the impression that you could move data between
DataSets as strings of XML using the GetXML() method and some other
method to get it back in, but I can't see how to pass the string
generated by the GetXML() method into some other DataSet object. I
know there's a ReadXML() method, but I can't figure that out at the
moment. I'd appreciate any help I could get on this.

Thanks,

-Quick

Well I sorta figured this out and I was trying to make it more
complicated that it was however I'm getting a strange accessed error
when trying to retrieve a DataSet object from the Web Service has
anyone seen this before? When I test the web service by going to it
directly it returns the dataset just w/o any problems, this only shows
up when I'm calling it from the client application. The error and code
are below:

/****Web Service Code****/
[WebMethod(BufferResponse=true,Description="Returns a DataSet")]
public DataSet GetFolderByName(string folderName)
{
DataSet ds = new DataSet();
....code that fills the data set...
return ds;
}

/****Client App Code ****/
localhost.qFolder folderList = new localhost.qFolder();
DataSet ds = new DataSet();
ds = folderList.GetFolderByName("TestFolder");
foreach (DataRow row in ds.Tables[0].Rows)
{
Response.Write(row[0].ToString() + " || " + row[1].ToString() + "<BR /
}

/****Error****/
The request failed with HTTP status 401: Access Denied.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The request failed with
HTTP status 401: Access Denied.

Source Error:

Line 35:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://
tempuri.org/GetFolderByName", RequestNamespace="http://tempuri.org/",
ResponseNamespace="http://tempuri.org/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
Line 36: public System.Data.DataSet GetFolderByName(string
folderName) {
Line 37: object[] results = this.Invoke("GetFolderByName",
new object[] {
Line 38: folderName});
Line 39: return ((System.Data.DataSet)(results[0]));

Source File: c:\inetpub\wwwroot\DataSetXML\Web References\localhost
\Reference.cs Line: 37

Stack Trace:

[WebException: The request failed with HTTP status 401: Access
Denied.]

System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage
message, WebResponse response, Stream responseStream, Boolean
asyncCall)
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String
methodName, Object[] parameters)
DataSetXML.localhost.qFolder.GetFolderByName(String folderName) in
c:\inetpub\wwwroot\DataSetXML\Web References\localhost\Reference.cs:37
DataSetXML.WebForm1.Page_Load(Object sender, EventArgs e) in c:
\inetpub\wwwroot\datasetxml\webform1.aspx.cs:26
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750
 
Q

Quick

Ok, please don't hold this against me, I know there are like 50 topics
out there about this, but none of them really address the basics of
this issue and I'm hoping there's someone around that can help.
I'm looking for help implementing a very basic example.
I have a web service with one web method that returns aDataSetthat
contains 1 DataTable.
I want to pass back anXMLrepresentation of theDataSetor DataTable
(don't care which) to a .Net web app that's going to consume the web
service.
Once theXMLgets passed to the client application i want to take it
and put it back into a DataTable.
Is there any reasonably straight forward way to accomplish this and if
so can someone give me an example?
For some reason I had the impression that you could move data between
DataSets as strings ofXMLusing the GetXML() method and some other
method to get it back in, but I can't see how to pass the string
generated by the GetXML() method into some otherDataSetobject. I
know there's a ReadXML() method, but I can't figure that out at the
moment. I'd appreciate any help I could get on this.

-Quick

Well I sorta figured this out and I was trying to make it more
complicated that it was however I'm getting a strange accessed error
when trying to retrieve aDataSetobject from the Web Service has
anyone seen this before? When I test the web service by going to it
directly it returns thedatasetjust w/o any problems, this only shows
up when I'm calling it from the client application. The error and code
are below:

/****Web Service Code****/
[WebMethod(BufferResponse=true,Description="Returns aDataSet")]
publicDataSetGetFolderByName(string folderName)
{
DataSetds = newDataSet();
...code that fills the data set...
return ds;

}

/****Client App Code ****/
localhost.qFolder folderList = new localhost.qFolder();DataSetds = newDataSet();
ds = folderList.GetFolderByName("TestFolder");
foreach (DataRow row in ds.Tables[0].Rows)
{
Response.Write(row[0].ToString() + " || " + row[1].ToString() + "<BR /
}

/****Error****/
The request failed with HTTP status 401:AccessDenied.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.Net.WebException: The request failed with
HTTP status 401:AccessDenied.

Source Error:

Line 35:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://
tempuri.org/GetFolderByName", RequestNamespace="http://tempuri.org/",
ResponseNamespace="http://tempuri.org/",
Use=System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
Line 36: public System.Data.DataSetGetFolderByName(string
folderName) {
Line 37: object[] results = this.Invoke("GetFolderByName",
new object[] {
Line 38: folderName});
Line 39: return ((System.Data.DataSet)(results[0]));

Source File: c:\inetpub\wwwroot\DataSetXML\Web References\localhost
\Reference.cs Line: 37

Stack Trace:

[WebException: The request failed with HTTP status 401:AccessDenied.]

System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage
message, WebResponse response, Stream responseStream, Boolean
asyncCall)
System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String
methodName, Object[] parameters)
DataSetXML.localhost.qFolder.GetFolderByName(String folderName) in
c:\inetpub\wwwroot\DataSetXML\Web References\localhost\Reference.cs:37
DataSetXML.WebForm1.Page_Load(Object sender, EventArgs e) in c:
\inetpub\wwwroot\datasetxml\webform1.aspx.cs:26
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032;
ASP.NET Version:1.1.4322.2032

-Quick

As a follow up, I've got Integrated Windows Authentication turned on
for both the WS site and the Client App Site. When I check the IIS
log files I can see that it's passing the correct network id around,
just can't see why the Access Denied is getting thrown...
 
Q

Quick

Here's the answer in case anyone else runs into this...

The reason I was getting the 401, was that the specific code being run
by the web service was reading the current user context and attempting
to authenticate based on that.

To tell your proxy to pass the context of the current user that is
making the request from the web service use the following code:

//instantiate your proxy (the web reference you made)
localhost.qFolder folderList = new localhost.qFolder();

//tell it to use the context of the current user for authentication
folderList.Credentials =
System.Net.CredentialCache.DefaultCredentials;

-Quick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top