【发布时间】:2014-09-24 22:16:20
【问题描述】:
我正在尝试创建 SVG 格式的对象模型。所以,由于它是 XML,我使用的是 XmlSerializer。但我有一个问题。有一些名为“样式”的 xml 属性。它看起来像复杂类型,但表示为字符串。有这个属性的例子:
style="fill:none;stroke:#00ff00;stroke-width:8.7489996;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:8.74900006"
如您所见,有“fill”、“stroke”、“stroke-width”等属性。 我写的课
public class SvgStyle
{
public string FillColor { get; set; }
public string StrokeColor { get; set; }
public float StrokeWidth { get; set; }
public int StrokeMiterlimit { get; set; }
public float StrokeOpacity { get; set; }
public float StrokeDashoffset { get; set; }
public string StrokeDasharray { get; set; }
}
和另一个班级
public abstract class SvgGraphicElement
{
[XmlAttribute("style")]
public SvgStyle Style { get; set; }
}
我得到的只是例外
无法序列化 Svg.SvgStyle 类型的成员“样式”。 XmlAttribute/XmlText 不能用于编码复杂类型。
我尝试使用 IXmlSerializable 接口实现和 OnSerializing/OnDeserializing 方法,但我得到的只是另一个例外。 有没有办法将此字符串反序列化到我的班级? 谢谢。
【问题讨论】: