473,385 Members | 2,180 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Receive and return bare XML in C#/ASP.NET Web Service

All I wanted to do was implement a web service where I'd receive an
XML document and return one in response. I'd already figured out how
to use XmlReader and XmlWriter classes to do the XML work I need to
do. And all that fancy ASP.NET serialization really got in my way.

Reading articles by Yasser Shohoud, Tim Ewald, and Matt Powell led me
down many promising paths, along the way writing an XSD schema file
for my messages, trying to generate a interface using wsdl.exe, and
even considered implementing a SOAP extension to get at the streams.
But once I read Powell's article on "The Power of XmlElement
Parameters in ASP.NET Web Methods" I realized I should be able to do
what I needed, once I sussed out the use of C# attributes.

Since it took me days to figure all this out I thought I should post
the (complete, compilable, code-behind) source for a simple 'echo' web
service that demonstrates this, to help those trying to do similar
things.

The only thing that this shows that wasn't in Powell's article is the
use of the [return: XmlAnyElement] attribute declaration on the web
method. This (at least in combination with the
SoapParameterStyle.Bare) causes the XML returned to be an immediate
child of the <soap:Body> element. Otherwise one gets an interposed
<echoResult> which, at least in my case, is undesirable.

Note that this results in very unrestrained WSDL for your operation.
From echo.asmx?wsdl:

<message name="echoSoapIn">
<part name="input" type="s0:any"/>
</message>
<message name="echoSoapOut">
<part name="echoResult" type="s0:any"/>
</message>

In my case, I'm going to use this as the basis for an intranet web
service, so I don't mind. And I do plan to parse the XML I receive
with XmlValidatingTextReader. But I *believe* using the
WebServiceBindingAttribute with a hand-crafted WSDL file one could
cause ASP.NET to do the validation while having the method still
receive the bare XML. But maybe not.....?

Cheers,
Richard

---- begin echo.asmx.cs ----

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;

namespace echo
{
[ WebService(
Description="echo web service - receives bare SOAP body and echoes
it back as SOAP body, without wrapping or encoding",
Namespace="http://localhost/echo") ]
public class echows : System.Web.Services.WebService
{
public echows() { }

[WebMethod]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
[return: XmlAnyElement]
public XmlDocument echo([XmlAnyElement]XmlElement input)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(input.OuterXml.ToString());
return xml;
}
}
}
---- end echo.asmx.cs ----

--
*** Richard A. Wells, ra*@raw.com
*** Reality And Wonder, http://www.raw.com/
Nov 15 '05 #1
2 14290
Hi Richard,

For this issue, you may refer to following article to see if it will help:

http://msdn.microsoft.com/library/de...us/dnsvcinter/
html/wsi-bp_chapter3.asp

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Nov 15 '05 #2
JD
Richard,

Have you been able to support Http POST without SOAP with XML documents?
Your sample only supports SOAP.

Thanks for posting,
James

"Richard A. Wells" <ra*@raw.com> wrote in message
news:fc**************************@posting.google.c om...
All I wanted to do was implement a web service where I'd receive an
XML document and return one in response. I'd already figured out how
to use XmlReader and XmlWriter classes to do the XML work I need to
do. And all that fancy ASP.NET serialization really got in my way.

Reading articles by Yasser Shohoud, Tim Ewald, and Matt Powell led me
down many promising paths, along the way writing an XSD schema file
for my messages, trying to generate a interface using wsdl.exe, and
even considered implementing a SOAP extension to get at the streams.
But once I read Powell's article on "The Power of XmlElement
Parameters in ASP.NET Web Methods" I realized I should be able to do
what I needed, once I sussed out the use of C# attributes.

Since it took me days to figure all this out I thought I should post
the (complete, compilable, code-behind) source for a simple 'echo' web
service that demonstrates this, to help those trying to do similar
things.

The only thing that this shows that wasn't in Powell's article is the
use of the [return: XmlAnyElement] attribute declaration on the web
method. This (at least in combination with the
SoapParameterStyle.Bare) causes the XML returned to be an immediate
child of the <soap:Body> element. Otherwise one gets an interposed
<echoResult> which, at least in my case, is undesirable.

Note that this results in very unrestrained WSDL for your operation.
From echo.asmx?wsdl:

<message name="echoSoapIn">
<part name="input" type="s0:any"/>
</message>
<message name="echoSoapOut">
<part name="echoResult" type="s0:any"/>
</message>

In my case, I'm going to use this as the basis for an intranet web
service, so I don't mind. And I do plan to parse the XML I receive
with XmlValidatingTextReader. But I *believe* using the
WebServiceBindingAttribute with a hand-crafted WSDL file one could
cause ASP.NET to do the validation while having the method still
receive the bare XML. But maybe not.....?

Cheers,
Richard

---- begin echo.asmx.cs ----

using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;

namespace echo
{
[ WebService(
Description="echo web service - receives bare SOAP body and echoes
it back as SOAP body, without wrapping or encoding",
Namespace="http://localhost/echo") ]
public class echows : System.Web.Services.WebService
{
public echows() { }

[WebMethod]
[SoapDocumentMethod(ParameterStyle = SoapParameterStyle.Bare)]
[return: XmlAnyElement]
public XmlDocument echo([XmlAnyElement]XmlElement input)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(input.OuterXml.ToString());
return xml;
}
}
}
---- end echo.asmx.cs ----

--
*** Richard A. Wells, ra*@raw.com
*** Reality And Wonder, http://www.raw.com/

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: das | last post by:
Hi When I try to post message on msdn newsgroup. Most of the time I receive this message Service temporary not available Even it takes hours to post a single message. is it a common problem...
1
by: Bob | last post by:
I'm implementing a file transfer application to transfer confidential files from our corporate intranet to our web server in the DMZ. I was able to use WSE 1.0 DIME attachment for this without...
2
by: Qindong Zhang | last post by:
My socket application blocked at socket.receiver() after received all information from sender. Should socket.Receive() return 0 after no more data available? Note: My socket object was not close on...
12
by: Noel | last post by:
Hello, I'm currently developing a web service that retrieves data from an employee table. I would like to send and retrieve a custom employee class to/from the webservice. I have currently coded...
0
by: rene.rugerio | last post by:
hi i want to consume a web service with the following restrictions - the web service is dinamically allocated (changing server and operation name). - i only receive from web service "posters"...
2
by: 6kjfsyg02 | last post by:
I am trying to return one of two different objects from the same method, but I can not do it in WSDL or C#. I have a web service with three methods. I have been told that one of the methods...
1
by: Sandy | last post by:
I am defining a web service to receive data that represents an application form and that returns a message indicating if the application form has been successfully validated or not, listing any...
9
by: xieliwei | last post by:
Hello, I'm attempting to write a WinNT service that basically changes the time zone and forces a resync with a time server on initialisation. The service will then restore the original time zone...
0
by: =?Utf-8?B?QW5keSBZdQ==?= | last post by:
Hi, I'm trying to return exceptions from a WCF Service using FaultExceptions. I got the service compiled and running. But I get an error while adding a service reference to it. The error reads: "...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.