Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive_Milestone2/sources/HeuristicLab.Persistence/3.3/Default/Xml/Primitive/String2XmlSerializer.cs @ 1835

Last change on this file since 1835 was 1823, checked in by epitzer, 16 years ago

Namespace refactoring: rename formatters & decomposers -> primitive and composite serializers. (#603)

File size: 1.2 KB
Line 
1using System;
2using HeuristicLab.Persistence.Core;
3using HeuristicLab.Persistence.Interfaces;
4using System.Text;
5using System.Text.RegularExpressions;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7using System.Globalization;
8
9
10namespace HeuristicLab.Persistence.Default.Xml.Primitive {
11
12  [EmptyStorableClass]
13  public class String2XmlSerializer : PrimitiveXmlSerializerBase<string> {
14
15    public override XmlString Format(string s) {
16      StringBuilder sb = new StringBuilder();
17      sb.Append("<![CDATA[");
18      sb.Append(s.Replace("]]>", "]]]]><![CDATA[>"));
19      sb.Append("]]>");
20      return new XmlString(sb.ToString());
21    }
22
23    public override string Parse(XmlString x) {
24      StringBuilder sb = new StringBuilder();
25      Regex re = new Regex(@"<!\[CDATA\[((?:[^]]|\](?!\]>))*)\]\]>", RegexOptions.Singleline);
26      foreach (Match m in re.Matches(x.Data)) {
27        sb.Append(m.Groups[1]);
28      }
29      string result = sb.ToString();
30      if (result.Length == 0 && x.Data.Length > 0 && !x.Data.Equals("<![CDATA[]]>"))
31        throw new PersistenceException("Invalid CDATA section during string parsing.");
32      return sb.ToString();
33    }
34  }
35}
Note: See TracBrowser for help on using the repository browser.