Returning an XElement from a web service

こ雲淡風輕ζ 提交于 2019-12-05 16:54:02

There appears to be an issue with how an XElement is serialized, check here...

You can try outing the XElement as a string or as the article suggests you could just use a class wrapper and place your XElement inside. If the point is to output the data in a universal format you'll be stuck with returning a string.

I was trying to avoid the string!

I can return an XmlNode (created from the XElement), which gets me what I need - a slug of XML on the client. Thanks for that link - I shall investigate the XWrapper further...

Strangely enough, it seems that this only happens if you are trying to look at the "Discovery" part of the service - You can run the method without a problem...

Return an XmlElement type, 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!