Returning an XElement from a web service

Is it possible to return an XElement from a web service (in C # / asp.net)?

Try a simple web service that returns an XElement:

[WebMethod]
public XElement DoItXElement()
{
  XElement xe = new XElement("hello",
     new XElement("message", "Hello World")
     );

  return xe;
}

      

This compiles fine, but if you try to run it you get

Cannot use wildcards at the top level of a schema.

I found this post implying that this is a bug in .net.

So ... can I return an XElement from a web service? If so, how?

Thank.

+4


source to share


4 answers


Seems to be a problem with XElement serialization, check here ...



You can try the XElement as a string, or as the article says, you can just use a wrapper around the class and put your XElement inside. If the point is to output data in a generic format, you will loop over returning a string.

+5


source


I was trying to avoid the line!



I can return an XmlNode (created from an XElement) which gets me what I need - the XML pool on the client. Thanks for this link - I'll be exploring XWrapper further ...

+3


source


Oddly enough, it looks like this only happens if you try to look at the "Discovery" part of the service - you can run the method without issue ...

0


source


Return type XmlElement and just use this extension method to convert from XElement to XmlElement:

[System.Runtime.CompilerServices.Extension()]
public XmlElement ToXmlElement(XElement value)
{
    var xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(value.ToString());
    return xmlDoc.DocumentElement;
}

      

This seems to work well.

0


source







All Articles