Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize a regular expression type using .NET XML serialization

Tags:

c#

.net

regex

xml

How can I serialize a string from XML into a class property of type Regex?

Here's the elements in the XML file:

  <IncludeRegex><![CDATA[#include\\s*\\\"\\s*(?<FileName>.+\\\\*\\.*.+)\\s*\\\"]]></IncludeRegex>
  <IncludeDefineRegex><![CDATA[#define\s*[A-Za-z_0-9]+\s*\""\s*(?<FileName>.+\.[a-zA-Z0-9]+)\s*\""]]></IncludeDefineRegex>

In my class, that I serialize, I have these properties:

public Regex IncludeRegex { get; set; }
public Regex IncludeDefineRegex { get; set; }

The rest of the class serializes without a problem, but the Regex properties fail to serialize. One option is to have alternate properties:

public string IncludeRegex { get; set; }
public string IncludeDefineRegex { get; set; }
[XmlIgnore]
public Regex IncludeRegexActual { get; private set; }

[XmlIgnore]
public Regex IncludeDefineRegexActual { get; private set; }

And set these properties in the setters of IncludeRegex and IncludeDefineRegex or an init function. This works, but can I do it without the duplicate properties?

like image 490
Michael Kelley Avatar asked Jun 01 '11 17:06

Michael Kelley


People also ask

What is the difference between binary serialization and XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.

What is XML serialization in C#?

XML serialization is the process of converting an object's public properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization re-creates the object in its original state from the XML output.

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

Which annotation is needed for serialization and deserialization of XML format in the model class?

Jackson annotations are useful in defining and controlling the process of serialization and deserialization across various formats such as XML, JSON, and YAML.


2 Answers

I don't think you can do that with regular XML serialization, you have to implement IXmlSerializable to control the serialization directly.

like image 161
aL3891 Avatar answered Oct 16 '22 09:10

aL3891


The problem is, that Regex cannot be serialized. That can be solved by implementing IXmlSerializable. As you cannot change the Regex class, you have to handle it in another class. You might be able to do something like this (untested, out of my head):

public class MyRegEx : Regex, IXmlSerializable {...}

Now you have to implement the interface and have to use that class instead of Regex.

like image 1
Achim Avatar answered Oct 16 '22 09:10

Achim