0

I am trying to serialize an xml document, and one of the nodes is giving me trouble. My xml document is:

<?xml version="1.0" encoding="utf-8"?>
<item id="tcm:38-21324" title="Accessibility Product Accessibility content - 11-INTL" type="Component">
  <title>Accessibility Product Accessibility content - 11-INTL</title>
  <generalContent xmlns="uuid:bc85180b-db18-412f-b7ad-36a25ff4012f">
    <title>Produkttilgængelighed</title>
    <style xlink:href="tcm:38-3149-1024" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Horizontal Rule (Blue)">Horizontal Rule (Blue)</style>
    <image>
      <image xlink:type="simple" xlink:href="tcm:38-33683" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Feature-Image_D_527478227_kp" />
      <smallImage xlink:type="simple" xlink:href="tcm:38-33684" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Feature-Image_M_527478227_kp" />
      <retinaImage xlink:type="simple" xlink:href="tcm:38-33685" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Feature-Image_T_527478227_kp" />
      <altText>Kvinde med briller kigger på en bærbar computer</altText>
      <title>Kvinde med briller kigger på en bærbar computer</title>
      <imagePosition>Right</imagePosition>
    </image>
    <description>
      <hr class="mainRow__hr mainRow__hr--bbOrange" xmlns="http://www.w3.org/1999/xhtml" />
      <p xmlns="http://www.w3.org/1999/xhtml">Vores produkter er designet og udviklet</p>
    </description>
  </generalContent>
</item>

And this is my model. It works fine with other fields except the description field.

    [Serializable()]
    [XmlRoot(ElementName = "generalContent", Namespace = "uuid:bc85180b-db18-412f-b7ad-36a25ff4012f")]
    public class GeneralContent
    {
        [XmlElement(ElementName = "title")]
        public string Title { get; set; }

        [XmlElement(ElementName = "image")]
        public Image Image { get; set; }

        [XmlElement(ElementName = "description")]
        public string Description { get; set; }

    }

the code that does the serialization is a generic one and works with all my other models.

   public static T MapXmlToType<T>(XElement xmlData) where T:ITridionModel
    {
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        return (T) xmlSerializer.Deserialize(xmlData.CreateReader());
    }

I need to get the "description" node as string. It is basically a rich text field that I need the whole content including the styling values. But, this model as it is right now throws System.InvalidOperationException for description node. How can I serialize this?

Payam
  • 741
  • 3
  • 14
  • 37
  • Please post your code serialization/deserialization code. Are you trying to serialize (write XML) or deserialize (read XML)? – Wagner DosAnjos Nov 15 '16 at 14:48
  • I think you will need to implement the serialization of the Description Property by hand and disable all rtf tags which get interpreted as xml or special character (see IXmlSerializable) – Sebi Nov 15 '16 at 14:48
  • erm, XML is serialized. – Jodrell Nov 15 '16 at 14:49
  • possible your rich text is html not rtf? – Sebi Nov 15 '16 at 14:49
  • It looks to me that you're separating the markup code from the text. But the description property doesn't know how to decode that. If you leave the markup in with the text as one string it should work fine. If necessary you can use a RichTextBox. The rtf property will return such a string. – tinstaafl Nov 15 '16 at 15:51

2 Answers2

0

all you need to do is in the model change the Description as follows:

   [XmlElement(ElementName = "description")]
   public XElement Description { get; set; }

   public string DescriptionAsText => Description.ToString();
Payam
  • 741
  • 3
  • 14
  • 37
-1

I had the same challenge and used a very easy workaround. Encrypt your RTF tagged text using another method like GZipStream or Base64. I used it with no problems but I'm not sure if it's encoding safe or may raise other problems.

You can also write a property in your model to support the conversion automatically like this:

[Serializable()]
    [XmlRoot(ElementName = "generalContent", Namespace = "uuid:bc85180b-db18-412f-b7ad-36a25ff4012f")]
    public class GeneralContent
    {
        [XmlElement(ElementName = "title")]
        public string Title { get; set; }

        [XmlElement(ElementName = "image")]
        public Image Image { get; set; }

        [XmlIgnore]
        public string Description { get; set; }

        [XmlElement(ElementName = "description")]
        public string EncryptedDescription
        {
           get { return Encrypt(Description); }
           set { Description = Decrypt(value); }
        }
    }

Hope it helps

Community
  • 1
  • 1
Emad
  • 3,809
  • 3
  • 32
  • 44