1
創建一個XML響應我要產生這種XML:如何在WCF REST
<data contentType="text/plain" contentLength="24">
<![CDATA[OK - 12/05/2016 14:45:40]]>
</data>
我的計劃行之有效,但我覺得必須有另一種方式來產生這種XML。
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Xml,
UriTemplate = "ping")]
Stream PingServer();
public Stream PingServer()
{
string LeUrl = "http://yyyyy.fr/Service1.svc";
string Result = "";
try
{
var myRequest = (HttpWebRequest)WebRequest.Create(LeUrl);
var response = (HttpWebResponse)myRequest.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
// it's at least in some way responsive
// but may be internally broken
// as you could find out if you called one of the methods for real
//Debug.Write(string.Format("{0} Available", url));
Result = "OKE --" + DateTime.Now ;
}
else
{
// well, at least it returned...
//Debug.Write(string.Format("{0} Returned, but with status: {1}", url, response.StatusDescription));
Result = response.StatusDescription;
}
}
catch (Exception ex)
{
// not available at all, for some reason
//Debug.Write(string.Format("{0} unavailable: {1}", url, ex.Message));
Result = ex.Message;
}
WebOperationContext CurrentWebContext = WebOperationContext.Current;
CurrentWebContext.OutgoingResponse.ContentType = "text/plain";
String AnyXml = "<data contentType=\"text/plain\" contentLength=\"24\">"+"><![CDATA[OK - "+DateTime.Now+"]]></data>";
return new MemoryStream(Encoding.UTF8.GetBytes(AnyXml));
}
我覺得使用XmlElement
或類似的東西。
我不想自己創建XML語法。