如何使用CData属性

本文关键字:属性 CData 何使用 | 更新日期: 2023-09-27 18:02:30

如何用CData序列化属性?我尝试了几种不同的方法,包括使原始属性XmlIgnore和引入返回XmlCDataSection的属性。到目前为止都没有成功。

我有以下显示错误的可运行控制台测试。我如何修改它以允许Regex数据序列化和反序列化?

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("&amp;", "&").Replace("&lt;", "<").Replace("&gt;", ">").Replace("&quot;", "'"").Replace("&apos;", "'"); }
        set { _regex = value.Replace(@"'", @"''").Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("'"", "&quot;").Replace("'", "&apos;"); }
    }

如何使用CData属性

你不需要在这里使用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();
    }
}