Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with Serialization/Deserialization an XML containing CDATA attribute

I need to deserialize/serialize the xml file below:

<items att1="val">
<item att1="image1.jpg">
         <![CDATA[<strong>Image 1</strong>]]>
</item>
<item att1="image2.jpg">
         <![CDATA[<strong>Image 2</strong>]]>
</item>     
</items>

my C# classes:

[Serializable]
[XmlRoot("items")]    
public class RootClass
{
  [XmlAttribute("att1")]
  public string Att1 {set; get;}

  [XmlElement("item")]  
  public Item[] ArrayOfItem {get; set;}
}

  [Serializable]
public class Item
{
    [XmlAttribute("att1")]
    public string Att1 { get; set; }

    [XmlText]
    public string Content { get; set; }
}

and everything works almost perfect but after deserialization in place

<![CDATA[<strong>Image 1</strong>]]>

I have

&lt;strong&gt;Image 1&lt;/strong&gt;

I was trying to use XmlCDataSection as type for Content property but this type is not allowed with XmlText attribute. Unfortunately I can't change XML structure.

How can I solve this issue?

like image 960
higi Avatar asked Dec 07 '25 09:12

higi


1 Answers

this should help

    private string content;

    [XmlText]
    public string Content
    {
        get { return content; }
        set { content = XElement.Parse(value).Value; }
    }
like image 75
Stecya Avatar answered Dec 09 '25 22:12

Stecya