2011-03-19 133 views
3

我需要反序列化/序列化下面的XML文件的XML:問題的序列化/反序列化包含CDATA屬性

<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結構。

我該如何解決這個問題?

+1

'<![CDATA [圖像1]]>'和'<強>圖像1 < /強>'是相同的東西。你的問題在哪裏? – Tomalak 2011-03-19 19:27:57

+0

讀取xml的另一個應用程序有一些問題'<強>圖片1 < /強>' – higi 2011-03-19 20:03:55

+0

這意味着這個其他應用程序不能理解XML,應該修復。 – Tomalak 2011-03-19 20:45:40

回答

1

這應該有助於

private string content; 

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

首先聲明一個屬性爲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> 
0

大多數在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); 
     } 
    }