Rant: Don't return XML in string variables!
I have probably seen a half a dozen article submissions to MSDN in the last year with authors who do the following:
// Bad Bad Bad
[WebMethod]
public string MyLameWebMethod()
{
XmlDocument dom = new XmlDocument();
// load some XML ...
return dom.OuterXml;
}
and then on the client side they do this:
// Bad bad bad
localhost.Service1 proxy = new localhost.Service1();
XmlDocument dom = new XmlDocument();
dom.LoadXml(proxy.MyLameWebMethod());
!!!DON'T DO THIS!!!
Try this instead:
// Better
[WebMethod]
public XmlDocument MyBetterWebMethod()
{
XmlDocument dom = new XmlDocument();
// load some XML ...
return dom;
}
Then the client will look like this:
// Better
localhost.Service1 proxy = new localhost.Service1();
XmlNode xml = proxy.MyBetterWebMethod();
Now you are avoiding an added level of serialization/deserialization. This applies to valid XML only!! Non-wellformed HTML should be passed as a string that is HTML encoded.
I will say that I have sympathy about this sort of thing and the tools often aren't much help, but if I can help it, MSDN will never have an article encouraging users to pass XML as an encoded string to/from a Web service.
By the way, I've been trying to think of a scenario where returning XML as a string to/from a Web service might make sense...without luck. If you have one, I would like to hear about it. I think it is safe to say that for the vast majority of situations, returning XML as an encoded string is not a good practice.
-Matt