2

我已经给出了一些与此类似的预定义 XML:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Points>
      <Point X="1.345" Y="7.45" />
      <Point X="1.123" Y="5.564" />
      <Point X="3.34" Y="2.5345" />
   </Points>
   <!-- and a bunch of other attributes and structures which are perfectly serialized and deserialized by the XmlSerializer -->
</Root>

我的目标是将其反序列化为List<System.Windows.Point>使用XmlSerializer实例,反之亦然。因此,我定义了如下类型:

[Serializable]
[XmlRoot("Root")]
public class RootClass
{
   public List<System.Windows.Point> Points { get; set; }
   /* and more properties */
}

我的问题是XmlSerializer将框架属性解释为XmlElement. 为此,它们只能按原样读取和写入,而不是作为所需的属性。

我想到的一个解决方案是定义一个自定义点类型,它定义了XmlAttribtueAttribute每个坐标属性。这个自定义点被映射到System.Windows.Point结构。如下所示:

[XmlIgnore]
public List<Point> Points { get; set; }

[XmlArray("Points")]
[XmlArrayItem("Point")]
public List<CustomSerializedPoint> CustomSerializedPoints
{
    get { return this.Points.ToCustomSerializedPointList(); }
    set { this.Points = value.ToPointList(); }
}

但是对于这个解决方案,我注意到,setter 永远不会被调用,并且XmlSerializer调用 getterCustomSerializedPoints大约五次。它期望有一个后备列表,每个调用都有相同的引用,并且永远不会为空。为了满足该要求,这对我来说不是解决方案,因为我需要将其保留List<CustomSerializedPoints>在内存中,仅用于使用属性而不是元素来编写点。

那么有人有更实用的解决方案吗?

另外我的XmlSerializer代码:

/* ... */
var serializer = new XmlSerializer(typeof(RootClass));
TextReader textReader = new StreamReader("file.xml");
(RootClass)serializer.Deserialize(textReader);
/* ... */
4

1 回答 1

4

您可以通过在运行时更改其序列化属性来更改类的序列化/反序列化方式。XmlAttributeOverrides类提供了这种可能性。以下示例代码正确反序列化您提供的 XML:

XmlAttributes xa = new XmlAttributes();
XmlAttributes ya = new XmlAttributes();

xa.XmlAttribute = new XmlAttributeAttribute("X");
ya.XmlAttribute = new XmlAttributeAttribute("Y");

XmlAttributeOverrides xao = new XmlAttributeOverrides();
xao.Add(typeof(System.Windows.Point), "X", xa);
xao.Add(typeof(System.Windows.Point), "Y", ya);

var serializer = new XmlSerializer(typeof(RootClass), xao);
TextReader textReader = new StreamReader("file.xml");

var result = (RootClass)serializer.Deserialize(textReader);
于 2013-10-18T00:04:45.713 回答