Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:56:14+00:00 2026-05-23T21:56:14+00:00

How can I serialize a property with CData? I have tried a few different

  • 0

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<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();
        }
    }
}

Additional: Attempted replace method – not working

    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;"); }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T21:56:15+00:00Added an answer on May 23, 2026 at 9:56 pm

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object I can serialize to XML without a problem. However when
How can I serialize a string from XML into a class property of type
I have some data in a C# DataSet object. I can serialize it right
I have a class with a property that can be an Image (i.e. an
How can I XML-serialize the following property: public class SomeCollection { [Xml???] public SomeClass
I have a MarshalByRefObject which I needed to serialize and store (in a database),
I am looking for a tool that can serialize and/or transform SQL Result Sets
I can't serialize it, therefore can't get byte[]. Network whiteboard with .net is impossible?)
In C# how can I serialize a List<int> to a byte[] in order to
What C-sharp type can I serialize to get JSON object with format name:[[1,2,3],[1,2,3],[1,2,3]] If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.