Do it the right way 

With all the talk going on I agree that this is bad:

[WebService]
public class MyWebService
{
   [WebMethod]
   public string GetSomeXML()
   {
     // get some xml
     return xml;
   }

}

And:

public class MyClient
{
   public static void Main()
   {
      MyWebService objWS = new MyWebService();
      string strXML = objWS.GetSomeXML();
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(strXml);
      // processing
   }
}

A lot better (in most situations) is:

[WebService]
public class MyWebService
{
   [WebMethod]
   public XmlDocument GetSomeXML()
   {
     // return xml
   }
}

The former scenario serializes the document directly into the output SOAP stream, therefore bypassing double string (de)serialization.

There are special cases, when one would like to bypass the other approach (passing XML as XmlDocument) on the server side. If you have all the data ready and want to pass it as quickly as humanly possible, without rehydrating the entire full blown DOM-capable object, you would use System.String (xsd:string in the XML world) and System.Text.StringBuilder to contacenate it.

If you don't know what to choose I propose this:

  • It is year 2004, therefore platform and tool support is available in a way that XML processing is not a limitation from the XSD type system -> platform type system conversion side. Therefore choose XmlDocument.
  • Choose XmlDocument.
  • Choose the string way if and only if you are expecting clients which have no other way to bridge/decouple the raw SOAP XML string into something programmatic inside your platform.

In any case, things will change in late July or early August.

Categories:  XML
Monday, 17 May 2004 18:17:32 (Central Europe Standard Time, UTC+01:00)  #    Comments

 

Monday, 31 May 2004 17:38:31 (Central Europe Standard Time, UTC+01:00)
Here's a better approach, and you don't have to choose DOM vs (XmlReader | XPathNavigator): http://weblogs.asp.net/cazzu/archive/2004/05/31/144922.aspx (How to return well-formed XML from WebServices without the DOM: from XmlReader and XPathNavigator)
All comments require the approval of the site owner before being displayed.
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

Live Comment Preview
Copyright © 2003-2024 , Matevž Gačnik
Recent Posts
RD / MVP
Feeds
RSS: Atom:
Archives
Categories
Blogroll
Legal

The opinions expressed herein are my own personal opinions and do not represent my company's view in any way.

My views often change.

This blog is just a collection of bytes.

Copyright © 2003-2024
Matevž Gačnik

Send mail to the author(s) E-mail