【发布时间】:2011-03-19 19:21:47
【问题描述】:
我需要反序列化/序列化下面的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>]]>
我有
<strong>Image 1</strong>
我尝试使用 XmlCDataSection 作为 Content 属性的类型,但 XmlText 属性不允许使用此类型。不幸的是,我无法更改 XML 结构。
我该如何解决这个问题?
【问题讨论】:
-
<![CDATA[<strong>Image 1</strong>]]>和&lt;strong&gt;Image 1&lt;/strong&gt;是一回事。你的问题在哪里? -
另一个读取 xml 的应用程序的“<strong>Image 1</strong>”有一些问题
-
这意味着这个其他应用程序无法理解 XML,应该修复。
-
@Tomalak 就像你说的那样,因为两个版本是相同的,所以这不是我的错;)另一个应用程序不起作用。我向这个应用程序的所有者报告了这个问题,我正在等待一些修补程序。谢谢!
标签: c# .net xml xml-serialization cdata