1
我正在创建一个XElement
对象,并且在使用Linq将多个元素添加到节点时遇到问题。Linq to XElement,多个参数
有对象的多个属性的列表:
class Point { int x; int y; ... }
List<Point> Points = new List<Point>()
{
new Point(1,2),
new Point(3,4)
};
我想这个列表转换为使用单独的构造平坦XElement
(一次性)。
我希望看到的:
<Points>
<x>1</x>
<y>2</y>
<x>3</x>
<y>4</y>
</Points>
我走到这一步,最远:
new XElement("Points",
Points.Select(a => new {
X = new XElement("x", a.x),
Y = new XElement("y", a.y)
});
其产生的嵌套列表,解析为一个单一的XElement
。
<Points>{ X = <x>1</x>, Y = <y>2</y> }{ X = <x>3</x>, Y = <y>4</y> }</Points>
(我需要保存的顺序,以便联合将无法正常工作)
我想要做的一切在构造函数中,像上面,并且不想手动加点,像这样:
XElement PointsXml = new XElement("Points");
foreach (var item in Points)
{
PointsXml.Add(item.x);
PointsXml.Add(item.y);
}
工作就像一个魅力,非常感谢!另外,'Points.Select(a => new [] ...'也起作用。 – user5226582