Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Marc Gravell has a solution <a href="http://www.eggheadcafe.com/conversation.aspx?messageid=32002122&amp;threadid=32002078" rel="noreferrer">here</a> for serializing CDATA sections.</p> <p>I have copied the code here for posterity.</p> <p><strong>updated:</strong> the previous example did not generate a valid schema, the XmlSchemaProviderAttribute and accompanying method will generate "xs:string" which works <a href="http://blogs.microsoft.co.il/blogs/dorony/archive/2011/01/29/serializing-a-string-as-cdata-in-wcf.aspx" rel="noreferrer">[more...]</a></p> <pre><code>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; } // "" =&gt; &lt;Node/&gt; // "Foo" =&gt; &lt;Node&gt;&lt;![CDATA[Foo]]&gt;&lt;/Node&gt; public void WriteXml(XmlWriter writer) { if (!string.IsNullOrEmpty(Value)) { writer.WriteCData(Value); } } // &lt;Node/&gt; =&gt; "" // &lt;Node&gt;&lt;/Node&gt; =&gt; "" // &lt;Node&gt;Foo&lt;/Node&gt; =&gt; "Foo" // &lt;Node&gt;&lt;![CDATA[Foo]]&gt;&lt;/Node&gt; =&gt; "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 = @"&amp;&lt;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)"); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload