在C#中序列化富文本

时间:2016-11-15 14:45:08

标签: c# xml xmlserializer

我正在尝试序列化一个xml文档,其中一个节点给了我麻烦。我的xml文档是:

<?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>

这是我的模特。它适用于除描述字段之外的其他字段。

    [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; }

    }

执行序列化的代码是通用代码,可以与我的所有其他模型一起使用。

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

我需要将“description”节点作为字符串。它基本上是一个富文本字段,我需要整个内容,包括样式值。但是,这个模型现在正在为描述节点抛出System.InvalidOperationException。我如何序列化?

2 个答案:

答案 0 :(得分:0)

您需要做的就是在模型中更改描述如下:

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

   public string DescriptionAsText => Description.ToString();

答案 1 :(得分:-1)

我遇到了同样的挑战并使用了一个非常简单的解决方法。使用其他方法加密您的RTF标记文字,例如GZipStreamBase64。我使用它没有任何问题,但我不确定它是编码安全还是可能引起其他问题。

您还可以在模型中编写属性以自动支持转换:

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

希望有所帮助