如何使用CData序列化属性?我尝试了一些不同的方法,包括制作原始属性XmlIgnore并引入一个返回XmlCDataSection的属性。到目前为止没有人工作过。
我有以下可运行的控制台测试,它显示错误。如何修改此选项以允许正则表达式数据序列化和反序列化。
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<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();
}
}
}
附加:尝试替换方法 - 无法正常工作
private string _regex;
public string Regex
{
get { return _regex.Replace(@"\\", @"\").Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace(""", "\"").Replace("'", "'"); }
set { _regex = value.Replace(@"\", @"\\").Replace("&", "&").Replace("<", "<").Replace(">", ">").Replace("\"", """).Replace("'", "'"); }
}
答案 0 :(得分:1)
你不需要在这里使用CData - 问题是你的Regex没有字符串“\ b”,它确实有\ u0008(BS)字符 - 这不是正则表达式中你需要的。如果你在MyRegex初始化中转义'\',它应该可以工作:
MyRegex original = new MyRegex { Regex = "\\b[1-3]{1}\\b#Must be a value of 1 to 3" };
此控制台应用程序已准备好运行,并且可以正常序列化数据(使用\ 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();
}
}