2016-05-12 12 views
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構文を作成したくありません。

答えて

0

XmlSerializerを使用して、適切なSystem.Xml.Serialization属性(XmlRoot、XmlAttributeなど)でデータ型を定義し、操作を[XmlSerializerFormat]と宣言できます。

次のコードは、シナリオの可能な実装を示しています。 CData(これはあなたが質問にあったもの)の使用を強要することに注意してください。しかし、あなたがそれを必要としないならば、クラスに追加のプロパティを持つ必要はありません。

public class StackOverflow_37187563 
{ 
    [XmlRoot(ElementName = "data", Namespace = "")] 
    public class Data 
    { 
     [XmlAttribute(AttributeName = "contentType")] 
     public string ContentType { get; set; } 
     [XmlAttribute(AttributeName = "contentLength")] 
     public int ContentLength { get; set; } 
     [XmlElement] 
     public XmlCDataSection MyCData 
     { 
      get { return new XmlDocument().CreateCDataSection(this.Value); } 
      set { this.Value = value.Value; } 
     } 
     [XmlIgnore] 
     public string Value { get; set; } 
    } 

    [ServiceContract] 
    public class MyService 
    { 
     [WebGet(ResponseFormat = WebMessageFormat.Xml), XmlSerializerFormat] 
     public Data PingServer() 
     { 
      return new Data 
      { 
       ContentLength = 24, 
       ContentType = "text/plain", 
       Value = "OK - 12/05/2016 14:45:40" 
      }; 
     } 
    } 

    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     WebServiceHost host = new WebServiceHost(typeof(MyService), new Uri(baseAddress)); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     WebClient c = new WebClient(); 
     Console.WriteLine(c.DownloadString(baseAddress + "/PingServer")); 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
}