簡體   English   中英

序列化/反序列化問題包含CDATA屬性的XML

[英]Problem with Serialization/Deserialization an XML containing CDATA attribute

我需要反序列化/序列化下面的xml文件:

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

我的C#課程:

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

一切都工作得非常完美,但在反序列化之后

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

我有

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

我試圖使用XmlCDataSection作為Content屬性的類型,但XmlText屬性不允許使用此類型。 不幸的是我無法改變XML結構。

我該如何解決這個問題?

這應該有所幫助

    private string content;

    [XmlText]
    public string Content
    {
        get { return content; }
        set { content = XElement.Parse(value).Value; }
    }

首先將屬性聲明為XmlCDataSection

public XmlCDataSection ProjectXml { get; set; }

在這種情況下,projectXml是一個字符串xml

ProjectXml = new XmlDocument().CreateCDataSection(projectXml);

當你序列化你的消息時,你會得到你很好的格式(通知)

<?xml version="1.0" encoding="utf-16"?>
<MessageBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="Message_ProjectStatusChanged">
  <ID>131</ID>
  <HandlerName>Plugin</HandlerName>
  <NumRetries>0</NumRetries>
  <TriggerXml><![CDATA[<?xml version="1.0" encoding="utf-8"?><TmData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Version="9.0.0" Date="2012-01-31T15:46:02.6003105" Format="1" AppVersion="10.2.0" Culture="en-US" UserID="0" UserRole=""><PROJECT></PROJECT></TmData>]]></TriggerXml>
  <MessageCreatedDate>2012-01-31T20:28:52.4843092Z</MessageCreatedDate>
  <MessageStatus>0</MessageStatus>
  <ProjectId>0</ProjectId>
  <UserGUID>8CDF581E44F54E8BAD60A4FAA8418070</UserGUID>
  <ProjectGUID>5E82456F42DC46DEBA07F114F647E969</ProjectGUID>
  <PriorStatus>0</PriorStatus>
  <NewStatus>3</NewStatus>
  <ActionDate>0001-01-01T00:00:00</ActionDate>
</MessageBase>

StackOverflow中提供的大多數解決方案僅適用於序列化,而不適用於反序列化。 這個將完成這項工作,如果您需要從代碼中獲取/設置值,請使用我添加的額外屬性PriceUrlByString。

    private XmlNode _priceUrl;
    [XmlElement("price_url")]
    public XmlNode PriceUrl
    {
        get
        {
            return _priceUrl;
        }
        set
        {
            _priceUrl = value;
        }
    }

    [XmlIgnore]
    public string PriceUrlByString
    {
        get
        {
            // Retrieves the content of the encapsulated CDATA
            return _priceUrl.Value;
        }

        set
        {
            // Encapsulate in a CDATA XmlNode
            XmlDocument xmlDocument = new XmlDocument();
            this._priceUrl = xmlDocument.CreateCDataSection(value);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM