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 to share
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 to share