【发布时间】:2015-04-09 15:16:16
【问题描述】:
所以我在父类下有一个不同 Xml 元素的列表,如下所示:
<Group>
<Circle Radius="5"/>
<Rectangle Height="10" Width="15"/>
<Rectangle Height="7" Width="8"/>
<Circle Radius="12"/>
</Group>
还有课程:
public class Group
{
[XmlElement("Circle")]
public List<Circle> Circles { get; set;}
[XmlElement("Rectangle")]
public List<Circle> Circles { get; set;}
}
public class Circle
{
// The position it was encountered in the list of elements
public int Position { get; set; }
[XmlAttribute("Radius")]
public string Radius { get; set;}
}
public class Rectangle
{
// The position it was encountered in the list of elements
public int Position { get; set; }
[XmlAttribute("Width")]
public string Width { get; set; }
[XmlAttribute("Length")]
public string Length { get; set; }
}
所以基本上我在这里要做的是将这些不同元素作为子元素出现在其父元素下的顺序保存,将它们的位置保存在整数位置中。我能想到的唯一解决方案是有一个静态变量来计算这些对象的实例化并通过使用所述变量的计数来设置位置。我想相信有一种“更清洁”的方式来处理这个问题,并希望有任何建议。
编辑: 为了更清楚地知道我要去哪里,我想获取两个列表(因为它们是具有不同属性的不同对象)并将它们共同转换为具有共享的另一个类基类。所以我有一个这些基类元素的列表,这些元素的顺序与它们在 xml 中声明的顺序相同,以便我可以按该顺序将它们绘制到屏幕上(想想堆栈面板)。因此,如果在 Xml 反序列化期间继承是可能的,如果它们可以自动解析为基类列表,也可以解决我的问题。
【问题讨论】:
-
你能发布一个你想要的输出的例子吗?
-
List<T>不是按顺序序列化和反序列化的吗? -
我已经更新了我的回复,对于歧义,我深表歉意。
标签: c# .net xml deserialization