How to CData property?
How can I serialize a property with CData? I have tried a few different methods including making the original property XmlIgnore and introducing a property which returns XmlCDataSection. None have worked so far.
I have the following runnable console test which shows the error. How can I modify this to allow the Regex data to serialize and deserialize.
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Xml;
using System.Xml.Serialization;
[Serializable]
public class MyRegex
{
public string Regex { get; set; }
}
public static class SerializerHelper<T>
{
public static string Serialize(T myobject)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, myobject);
string xml = stringWriter.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
public static T DeSerialize(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}
class Program
{
static void Main(string[] args)
{
MyRegex original = new MyRegex { Regex = "\b[1-3]{1}\b#Must be a value of 1 to 3" };
string xml = SerializerHelper<MyRege开发者_运维知识库x>.Serialize(original);
Console.WriteLine("--- SERIALIZE ---");
Console.WriteLine(xml);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("--- DESERIALIZE ---");
MyRegex deSerial = SerializerHelper<MyRegex>.DeSerialize(xml);
Console.WriteLine("Equals? " + (deSerial.Regex.Equals(original.Regex)));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Console.ReadKey();");
Console.ReadKey();
}
}
}
Additional: Attempted replace method - not working
private string _regex;
public string Regex
{
get { return _regex.Replace(@"\\", @"\").Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'"); }
set { _regex = value.Replace(@"\", @"\\").Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); }
}
You don't need to use CData here - the problem is that your Regex does not have the string "\b", it does have the \u0008 (BS) character - which is not what you need in the regular expression. If you escape the '\' in the MyRegex initialization, it should work:
MyRegex original = new MyRegex { Regex = "\\b[1-3]{1}\\b#Must be a value of 1 to 3" };
This console app is ready to run, and it serializes the data fine (using \b):
public class StackOverflow_6755014
{
public class MyRegex
{
public string Regex { get; set; }
}
public static class SerializerHelper<T>
{
public static string Serialize(T myobject)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, myobject);
string xml = stringWriter.ToString();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
public static T DeSerialize(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}
public static void Test()
{
MyRegex original = new MyRegex { Regex = "\\b[1-3]{1}\\b#Must be a value of 1 to 3" };
string xml = SerializerHelper<MyRegex>.Serialize(original);
Console.WriteLine("--- SERIALIZE ---");
Console.WriteLine(xml);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("--- DESERIALIZE ---");
MyRegex deSerial = SerializerHelper<MyRegex>.DeSerialize(xml);
Console.WriteLine("Equals? " + (deSerial.Regex.Equals(original.Regex)));
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("Console.ReadKey();");
Console.ReadKey();
}
}
精彩评论