Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

I've been wondering why the class designs between ASMX Web services and System.Xml seem to be so disconnected. It seems like the ASMX team really doesn't get to keep up with the way things are done in System.Xml.

Take the feature that you can return an XmlDocument object from a Web serivce for example. It's great that you can fully control the response of a [WebMethod] ... if just the XmlDocument was the prescribed way of working with XML in the .NET Framework.

The XmlDocument really only works well if you write the code to modify the document's content. The classes in the .NET Framework that return XML results, like the XslTransform or an ADO.NET SqlCommand work with XmlWriters and XmlReaders. If you wanted to write the XML results from a SQL Server 2000 query that you obtained by calling ExecuteXmlReader to the response of a [WebMethod] you would first have to load the contents of the reader into an XmlDocument object:

[WebMethod]
[ … more serialization attributes ]
public XmlDocument GetData( … parameters )
{
 
  XmlDocument doc = new XmlDocument();
   SqlCommand cmd = new SqlCommand();  

   // populate SqlCommand
   // …

   XmlReader reader = cmd.ExecuteXmlReader();

   // now load the XML into an XmlDocument
   doc.Load( reader );
   return doc;
}

It seems to me that it shouldn’t be necessary to parse the content of the XmlReader and construct a DOM just in order to stream it back to the client. Wouldn’t Web services that employ SQL Server XML features and XSLT perform better without the extra, unnecessary document parsing step? 

Unfortunately, the new XslCommand and the XQueryCommand follow the API pattern of the XslTransform and it’s not possible to generate a Web service response in one single step without the DOM parsing. I would expect that especially with large XML documents, parsing the XML imposes a significant amount of processing and resource consumption on the server that could be avoided. With the API pattern of the XslTransform, you can write to an XmlDocument with Chris Lovett’s XmlNodeWriter from GotDotNet, but that’s going outside the .NET Framework and it's probably not well known to the average developer:

[WebMethod]
[ … more serialization attributes ]
public XmlDocument GetDataFromTransformation( … parameters )
{
 
  XmlDocument doc = new XmlDocument();
   XslTransform trafo = new XslTransform();
   XmlNodeWriter writer = new XmlNodeWriter( doc );

   // load XslTransform
   // …

   trafo.Transform( sourceXml, (XsltArgumentList) null, writer, (XmlResolver) null ); 
   return doc;
}

The approach with the XmlNodeWriter works OK, except that there have been some recommendation in the past to favor other options. I would much rather see a lighter weight XmlWriter than the XmlNodeWriter and the underlying XmlDocument if all I want to do is to stream an XML back to a Web service client.

In Whidbey things unfortunately don't change for the better. You can now create an XmlWriter to write the results of an XslCommand or an XQueryCommand to an XPath(Editable)Document, but that doesn’t derive from XmlNode, so yet again, you need to go not follow the guidance that the XPathDocument is the preferred XML API to work with and write the results to an XmlDocument (still via the XmlNodeWriter) or, if you need to use XPathDocument for other reasons, you have to add an extra (potentially expensive) step transferring the XML from the XPathDocument to an XmlDocument.

With Whidbey I was hoping to see some of the following improvements:

  • returning an XmlReader from a [WebMethod] should write the contents of the XmlReader directly to the response body, just like it works with the XmlNode derived classes today to integrate better with SqlCommand and other APIs that return XML through an XmlReader:

    [WebMethod]
    [ … more XmlSerialization attributes ]
    public XmlReader GetData()
    {
       SqlCommand cmd = new SqlCommand();
       // populate the command
       
       // now return the XML results
       return cmd.ExecuteXmlReader();
    }
  • Allow a [WebMethod] to return an XPathDocument avoid any unnecessary document serialization and parsing steps, to leverage any improvements of the new XPathNavigator and to enforce the message that XPathDocument objects are the preferred approach to pass around XML data in the .NET Framework
  • Make available an XmlWriter (possibly one that filters PIs to stay SOAP compliant) from within a WebMethod that one can pass directly to an XQueryCommand where the XML written to the XmlWriter is directly transferred to the response of the [WebMethod]

Unfortunately, when I submitted the suggestions at the Product Feedback Center, I got a response that these feature suggestions wouldn't be considered for Whidbey. I guess I have to wait another 3 years or so until we see consistent use of recommened practices between System.Xml and ASMX. Feel free to go and vote for my suggestion if you feel like me and you would really like to see these features in Whidbey.

Until then we can use the XmlNodeWriter approach outlined above or Cazzu's approach based on the XPathNavigator, or a couple or other approaches that I might blog about later.

Published Monday, September 06, 2004 2:04 PM by ChristophDotNet

Comments

# RE: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Monday, September 06, 2004 7:22 PM by kpako@yahoo.com (Dare Obasanjo)
I guess you haven't been reading my blog. See http://blogs.msdn.com/dareobasanjo/archive/2004/08/25/220251.aspx and http://blogs.msdn.com/dareobasanjo/archive/2004/09/03/225070.aspx

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Monday, September 06, 2004 8:11 PM by Christoph
Dare,

Thank you for the comments. I do read your blog, but I fell a little behind lately. Your recent entries do address some of the concerns I was raising, but not all of them. They don’t address the questions about getting XML from an XmlReader, as you would get it from ExecuteXmlReader for example, into the body of a Web service method.

With regard to the guidance that XPath navigator is the way to go, you’re now saying:

This was really an untenable situation. For this reason we've reverted the XPathDocument to what it was in v1.1 while new functionality and perf improvements will be made to the XmlDocument.

Does that imply that you are adding the CreateWriter method that’s on the XPathDocument in the Beta 1 bits will move to the XmlDocument class? This would address most of the scenarios that I thought about. Hopefully WSE 3 will build against Whidbey and then we have System.Xml, ASMX and WSE all happily working together. The exception then is still ExecuteXmlReader, but the importance of that call hopefully somewhat diminishes with XQuery becoming available in Whidbey as well.

Furthermore, you’re saying:

So in Whidbey, the XPathNavigator will be the programming model for working with XML data sources when one wants to abstract away from the underlying source[...]

Following that model, it should be very intuitive return an XPathNavigator that you’re using to abstract the data source from a WebMethod. Imagine that you’re passing an XPathNavigator around, for example between the various tiers of an application. There is still no intuitive, single-step to return the XPathNavigator from a [WebMethod]. I believe that limits the usablity of the XPathNavigator. The fact that I now have an editable XPathNavigator and the (historically editable) XmlDocument will add to the confusion of many developers out there. Can you at least add clear guidance on when which one is appropriate to the Whidbey docs?

Generally, I do think that System.Xml V2 is much more cohesive – at least within itself -- than V1 was! Keep up the good work.

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Thursday, April 28, 2011 2:59 AM by buy facebook likes

This is really my very first time here, great looking blog. I discovered so many interesting things inside your blog especially its discussion. From all the remarks in your articles, it appears such as this is often a very popular website. Keep up the truly amazing work.

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Wednesday, July 20, 2011 11:26 PM by Birkenstock sandals

Very nice and helpful information has been given in this article. I must say that this is a very good post.

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Monday, August 08, 2011 9:20 PM by buy designer handbags

Just want to say your article is brilliant. The clarity in your post is simply impressive and i can assume you are an expert on this subject.

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Saturday, August 13, 2011 2:27 AM by Cheap Louis Vuitton Outlet

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.<a href="www.cheaplouisvuittonsoutlets.com/" title="Cheap Louis Vuitton Outlet">Cheap Louis Vuitton Outlet</a>

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Monday, August 15, 2011 10:50 PM by cheap louis vuitton bags

Wonderful learn, I simply passed this onto a colleague who was in search of this info. I've you bookmarked on Digg.<a href="www.cheaplouisvuittonbagsonline.com/" title="cheap louis vuitton bags">cheap louis vuitton bags</a>

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Wednesday, August 17, 2011 2:52 AM by buy nike air max

I loved reading this article  I will be sure to tell my friends about this and link to it as well. Thanks!<a href="www.buynike-airmax.com/" title="buy nike air max">buy nike air max</a>

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Wednesday, August 17, 2011 9:51 PM by Canada Goose sale

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog.<a href="www.canadiangooseonsale.com/" title="Canada Goose sale">Canada Goose sale</a>

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Thursday, August 18, 2011 4:56 AM by Ralph Lauren outlet

Iam glad l came across this post, very educating. I will subscribe to your rss for future update.<a href="www.ralph-laurenoutletonline.com/" title="Ralph Lauren outlet">Ralph Lauren outlet</a>

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Saturday, September 03, 2011 2:56 AM by cheap prada handbags

I am glad to found such useful post. I really increased my knowledge after read your post which will be beneficial for me.<a href="www.cheappradahandbagsonline.com/" title="cheap prada handbags">cheap prada handbags</a>

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Saturday, September 03, 2011 9:41 PM by Monster beats studio

You're probably sick of hearing it, but you've got a really well written blog. Keep up the the great work.

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Thursday, September 08, 2011 3:47 AM by baseball caps

Thanks for taking this opportunity to converse about  this, I feel strongly  about this and I enjoy learning about this subject.

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Sunday, September 25, 2011 5:05 PM by dGgbVlYNuQWdz

WlNXVm Fresh thoughts, fresh view on the subject..!

# re: Interface inconsistencies between ASMX Web services and System.Xml V1 and V2

Thursday, December 08, 2011 1:36 AM by Sam Davis

Its really nice post thanks for share

Leave a Comment

(required) 
(required) 
(optional)
(required)