By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,245 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,245 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.