【发布时间】:2016-10-30 06:24:37
【问题描述】:
我有一个ColorFormat 类,用于存储有关颜色格式的基本信息。目的是能够对 XML 进行序列化和反序列化。为了表示红色、绿色和蓝色,我使用了特殊的颜色字符串标识符:
public const string RedColorIdentifier = "&red;";
public const string GreenColorIdentifier = "&green;";
public const string BlueColorIdentifier = "&blue;";
对于像“#RGB”这样的格式,类格式字符串是这样的:
colorFormat.Format = "#" + ColorFormat.RedColorIdentifier +
ColorFormat.GreenColorIdentifier +
ColorFormat.BlueColorIdentifier;
理想情况下,序列化的 XML 应该是:
<ColorFormat Name="HexFmt" ColorBase="Hex">#&red;&green;&blue;</ColorFormat>
实际的序列化是:
<ColorFormat Name="HexFmt" ColorBase="Hex">#&red;&green;&blue;</ColorFormat>
我想知道是否有一种方法可以“序列化和反序列化”您自己的自定义特殊 XML 字符
【问题讨论】:
-
你可以使用
CDATACDATA Sections -
XML
<ColorFormat Name="HexFmt" ColorBase="Hex">#&red;&green;&blue;</ColorFormat>无效。例如。上传到xmlvalidation.com,你会得到一个错误Errors in the XML document: 1:50 The entity "red" was referenced, but not declared. -
&text;- 是 xml entity。不要使用这样的实体。使用任何其他符号代替&。
标签: c# xml serialization deserialization