使用CDATA与WCF REST入门套件(Using CDATA with WCF REST sta

2019-07-04 15:43发布

我使用WCF REST入门工具包预览2服务建的,但我已经与各地XML风格的数据传入呼叫遇到的问题。 这是我的请求对象:

[DataContract(Namespace = "")]
public class ServiceRequest
{
    [DataMember]
    public string ContentText { get; set; }
    [DataMember]
    public string ApiKey { get; set; }

}

直到你在那里扔'一切正常。 是否有一个封装在CDATA或类似的东西ContentText财产?

Answer 1:

马克Gravell有一个解决方案在这里进行序列化CDATA部分。

我在这里复制的代码为后人。

更新:前面的例子中并没有产生一个有效的模式,在XmlSchemaProviderAttribute和所附的方法会产生“XS:字符串”,它的工作原理[更多...]

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;

[XmlSchemaProvider("GenerateSchema")]
public sealed class CDataWrapper : IXmlSerializable
{
  // implicit to/from string
  public static implicit operator string(CDataWrapper value)
  {
    return value == null ? null : value.Value;
  }

  public static implicit operator CDataWrapper(string value)
  {
    return value == null ? null : new CDataWrapper { Value = value };
  }

  public System.Xml.Schema.XmlSchema GetSchema()
  {
    return null;
  }

  // return "xs:string" as the type in scheme generation
  public static XmlQualifiedName GenerateSchema(XmlSchemaSet xs)
  {
      return XmlSchemaType.GetBuiltInSimpleType(XmlTypeCode.String).QualifiedName;
  }

  // "" => <Node/>
  // "Foo" => <Node><![CDATA[Foo]]></Node>
  public void WriteXml(XmlWriter writer)
  {
    if (!string.IsNullOrEmpty(Value))
    {
      writer.WriteCData(Value);
    }
  }

  // <Node/> => ""
  // <Node></Node> => ""
  // <Node>Foo</Node> => "Foo"
  // <Node><![CDATA[Foo]]></Node> => "Foo"
  public void ReadXml(XmlReader reader)
  {
    if (reader.IsEmptyElement)
    {
      Value = "";
    }
    else
    {
      reader.Read();

      switch (reader.NodeType)
      {
        case XmlNodeType.EndElement:
          Value = ""; // empty after all...
          break;
        case XmlNodeType.Text:
        case XmlNodeType.CDATA:
          Value = reader.ReadContentAsString();
          break;
        default:
          throw new InvalidOperationException("Expected text/cdata");
      }
    }
  }

  // underlying value
  public string Value { get; set; }
  public override string ToString()
  {
    return Value;
  }
}

// example usage
[DataContract(Namespace="http://myobjects/")]
public sealed class MyType
{
  public string SomeValue { get; set; }
  [DataMember(Name = "SomeValue", EmitDefaultValue = false)]
  private CDataWrapper SomeValueCData
  {
    get { return SomeValue; }
    set { SomeValue = value; }
  }

  public string EmptyTest { get; set; }
  [DataMember(Name = "EmptyTest", EmitDefaultValue = false)]
  private CDataWrapper EmptyTestCData
  {
    get { return EmptyTest; }
    set { EmptyTest = value; }
  }

  public string NullTest { get; set; }
  [DataMember(Name = "NullTest", EmitDefaultValue = false)]
  private CDataWrapper NullTestCData
  {
    get { return NullTest ; }
    set { NullTest = value; }
  }
}

// test rig
static class Program
{
  static void Main()
  {
    DataContractSerializer dcs = new DataContractSerializer(typeof(MyType));

    StringWriter writer = new StringWriter();
    using (XmlWriter xw = XmlWriter.Create(writer))
    {
      MyType foo = new MyType
      {
        SomeValue = @"&<t\d",
        NullTest = null,
        EmptyTest = ""
      };

      ShowObject("Original", foo);

      dcs.WriteObject(xw, foo);
      xw.Close();
    }

    string xml = writer.ToString();
    ShowObject("Xml", xml);

    StringReader reader = new StringReader(xml);
    using (XmlReader xr = XmlReader.Create(reader))
    {
      MyType bar = (MyType) dcs.ReadObject(xr);
      ShowObject("Recreated", bar);
    }
  }

  static void ShowObject(string caption, object obj)
  {
    Console.WriteLine();
    Console.WriteLine("** {0} **", caption );
    Console.WriteLine();

    if (obj == null)
    {
      Console.WriteLine("(null)");
    }
    else if (obj is string)
    {
      Console.WriteLine((string)obj);
    }
    else
    {
      foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(obj))
      {
        Console.WriteLine("{0}:\t{1}", prop.Name, prop.GetValue(obj) ?? "(null)");
      }
    }
  }
}


Answer 2:

在接受答案的CDataWrapper的VB转换:

Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Xml
Imports System.Xml.Schema
Imports System.Xml.Serialization
Imports System.ComponentModel

Public Class CDataWrapper
    Implements IXmlSerializable

    'underlying value
    Public Property Value As String

    'Implicit to/from string
    Public Shared Widening Operator CType(ByVal value As CDataWrapper) As String
        If value Is Nothing Then
            Return Nothing
        Else
            Return value.Value
        End If
    End Operator

    Public Shared Widening Operator CType(value As String) As CDataWrapper
        If value Is Nothing Then
            Return Nothing
        Else
            Return New CDataWrapper() With {.Value = value}
        End If
    End Operator


    Public Function GetSchema() As XmlSchema Implements IXmlSerializable.GetSchema
        Return Nothing
    End Function

    ' <Node/> => ""
    ' <Node></Node> => ""
    ' <Node>Foo</Node> => "Foo"
    ' <Node><![CDATA[Foo]]></Node> => "Foo"
    Public Sub ReadXml(reader As XmlReader) Implements IXmlSerializable.ReadXml
        If reader.IsEmptyElement Then
            Me.Value = ""
        Else
            reader.Read()

            Select Case reader.NodeType
                Case XmlNodeType.EndElement
                    Me.Value = "" ' empty after all...
                Case XmlNodeType.Text, XmlNodeType.CDATA
                    Me.Value = reader.ReadContentAsString()
                Case Else
                    Throw New InvalidOperationException("Expected text/cdata")
            End Select
        End If
    End Sub

    ' "" => <Node/>
    ' "Foo" => <Node><![CDATA[Foo]]></Node>
    Public Sub WriteXml(writer As XmlWriter) Implements IXmlSerializable.WriteXml
        If Not String.IsNullOrEmpty(Me.Value) Then
            writer.WriteCData(Me.Value)
        End If
    End Sub

    Public Overrides Function ToString() As String
        Return Me.Value
    End Function
End Class


Answer 3:

上面的代码是缺少,你必须走过去的内容您已经阅读后的事实。 所以这个类,因为它代表将无法与收集工作。

将其更改为以下内容,您现在可以持有的集合CDataWrapper

Value = reader.ReadContentAsString(); 
reader.Read();


Answer 4:

虽然这是在这里的旧帖子是我的2¢。 我通过定义数据成员作为XmlElement的解决了这个问题。

 [DataMember(Name = "MyCData")]
        public XmlElement MyCDataField { get; set; }


文章来源: Using CDATA with WCF REST starter kits
标签: c# wcf rest