C# 如何使用linq到xml创建多态数组?
是否可以使用LINQtoXML创建多态类型的数组 我有3个类,Base、Der1和Der2。Base是Der1和Der2的基类。我有一个xml文件,其中有对应于Der1和Der2对象的节点。我要做的是解析该文件并用Der1和Der2对象填充一个列表 xml将如下所示:C# 如何使用linq到xml创建多态数组?,c#,.net,xml,linq,C#,.net,Xml,Linq,是否可以使用LINQtoXML创建多态类型的数组 我有3个类,Base、Der1和Der2。Base是Der1和Der2的基类。我有一个xml文件,其中有对应于Der1和Der2对象的节点。我要做的是解析该文件并用Der1和Der2对象填充一个列表 xml将如下所示: <nodes> <node type = "Der1" attr1="val1" /> <node type = "Der2" attr2="val2" /> </node
<nodes>
<node type = "Der1" attr1="val1" />
<node type = "Der2" attr2="val2" />
</nodes>
我试图做但不起作用的是:
List<Base> PMList =
(from der1node from xmlDoc.Descendants("nodes")
where der1node.type == ("Der1")
select new Der1()
{
Attr1 = der1node.Attribute("attr1").Value
}
).Union<Base>
(from der2node from xmlDoc.Descendants("baseNode")
where der2node.type == ("Der2")
select new Der2()
{
Attr2 = der2node.Attribute("attr2").Value
}
).ToList<Base>();
List PMList=
(来自xmlDoc.substands(“节点”)中的der1node)
其中der1node.type==(“Der1”)
选择newder1()
{
Attr1=der1node.Attribute(“Attr1”).Value
}
).工会
(来自xmlDoc.substands(“baseNode”)的der2node)
其中der2node.type==(“Der2”)
选择newder2()
{
Attr2=der2node.Attribute(“Attr2”).Value
}
).ToList();
在这里,我试图构造类型为Der1的Der1对象和类型为Der2的Der2对象,并使用Union将它们添加到一个列表中
然而,这不起作用。如何使用LINQ to XML获取不同类型的对象并将它们放在一个多态集合中?一种方法是在投影它们之前将新实例强制转换到它们的基类:
List<Base> PMList = (from baseNode from xmlDoc.Descendants("nodes")
where baseNode.type == ("Der1")
select (Base) new Der1() {
Attr1 = baseNode.Attribute("attr1").Value
}
).Union(
from baseNode from xmlDoc.Descendants("baseNode")
where baseNode.type == ("Der2")
select (Base) new Der2() {
Attr2 = baseNode.Attribute("attr2").Value
}
).ToList();
List PMList=(来自xmlDoc.substands(“节点”)的baseNode)
其中baseNode.type==(“Der1”)
选择(基本)新的Der1(){
Attr1=baseNode.Attribute(“Attr1”).Value
}
).工会(
来自xmlDoc.substands的baseNode(“baseNode”)
其中baseNode.type==(“Der2”)
选择(基本)新的Der2(){
Attr2=baseNode.Attribute(“Attr2”).Value
}
).ToList();
一种方法是在投影新实例之前将其强制转换为基类:
List<Base> PMList = (from baseNode from xmlDoc.Descendants("nodes")
where baseNode.type == ("Der1")
select (Base) new Der1() {
Attr1 = baseNode.Attribute("attr1").Value
}
).Union(
from baseNode from xmlDoc.Descendants("baseNode")
where baseNode.type == ("Der2")
select (Base) new Der2() {
Attr2 = baseNode.Attribute("attr2").Value
}
).ToList();
List PMList=(来自xmlDoc.substands(“节点”)的baseNode)
其中baseNode.type==(“Der1”)
选择(基本)新的Der1(){
Attr1=baseNode.Attribute(“Attr1”).Value
}
).工会(
来自xmlDoc.substands的baseNode(“baseNode”)
其中baseNode.type==(“Der2”)
选择(基本)新的Der2(){
Attr2=baseNode.Attribute(“Attr2”).Value
}
).ToList();
将对象窗体选择强制转换为基本类型:
(from der1node from xmlDoc.Descendants("nodes")
where der1node.type == ("Der1")
select new Der1()
{
Attr1 = der1node.Attribute("attr1").Value
} as Base
)
将对象窗体强制转换为基本类型:
(from der1node from xmlDoc.Descendants("nodes")
where der1node.type == ("Der1")
select new Der1()
{
Attr1 = der1node.Attribute("attr1").Value
} as Base
)
创建序列时,只需确保将投影投射到基类型,使其成为
IEnumerable
而不是IEnumerable
List PMList=
(来自xmlDoc.substands(“节点”)中的der1node)
其中der1node.type==(“Der1”)
选择newder1()
{
Attr1=der1node.Attribute(“Attr1”).Value
}作为基础)(
(来自xmlDoc.substands(“baseNode”)的der2node)
其中der2node.type==(“Der2”)
选择newder2()
{
Attr2=der2node.Attribute(“Attr2”).Value
}as Base().ToList();
或者,您可以事先对序列调用Cast()
List<Base> PMList =
(from der1node from xmlDoc.Descendants("nodes")
where der1node.type == ("Der1")
select new Der1()
{
Attr1 = der1node.Attribute("attr1").Value
}).Cast<Base>().Union(
(from der2node from xmlDoc.Descendants("baseNode")
where der2node.type == ("Der2")
select new Der2()
{
Attr2 = der2node.Attribute("attr2").Value
}).Cast<Base>()).ToList();
List PMList=
(来自xmlDoc.substands(“节点”)中的der1node)
其中der1node.type==(“Der1”)
选择newder1()
{
Attr1=der1node.Attribute(“Attr1”).Value
}).Cast().Union(
(来自xmlDoc.substands(“baseNode”)的der2node)
其中der2node.type==(“Der2”)
选择newder2()
{
Attr2=der2node.Attribute(“Attr2”).Value
}).Cast()).ToList();
创建序列时,只需确保将投影投射到基类型,使其成为IEnumerable
而不是IEnumerable
List PMList=
(来自xmlDoc.substands(“节点”)中的der1node)
其中der1node.type==(“Der1”)
选择newder1()
{
Attr1=der1node.Attribute(“Attr1”).Value
}作为基础)(
(来自xmlDoc.substands(“baseNode”)的der2node)
其中der2node.type==(“Der2”)
选择newder2()
{
Attr2=der2node.Attribute(“Attr2”).Value
}as Base().ToList();
或者,您可以事先对序列调用Cast()
List<Base> PMList =
(from der1node from xmlDoc.Descendants("nodes")
where der1node.type == ("Der1")
select new Der1()
{
Attr1 = der1node.Attribute("attr1").Value
}).Cast<Base>().Union(
(from der2node from xmlDoc.Descendants("baseNode")
where der2node.type == ("Der2")
select new Der2()
{
Attr2 = der2node.Attribute("attr2").Value
}).Cast<Base>()).ToList();
List PMList=
(来自xmlDoc.substands(“节点”)中的der1node)
其中der1node.type==(“Der1”)
选择newder1()
{
Attr1=der1node.Attribute(“Attr1”).Value
}).Cast().Union(
(来自xmlDoc.substands(“baseNode”)的der2node)
其中der2node.type==(“Der2”)
选择newder2()
{
Attr2=der2node.Attribute(“Attr2”).Value
}).Cast()).ToList();
在附注中,你考虑使用而不是联合吗?Union将删除重复项,在您当前的状态下不应出现此问题。
但是,如果在某个时刻覆盖了Der1
和Der2
的相等性,则某些项可能会突然停止出现在列表中。意识到这一点很好
无论如何,我会这样做:
var items = xmlDoc.Descendants("nodes")
.Where(d1 => d1.type == ("Der1"))
.Cast<Base>()
.Concat(xmlDoc.Descendants("nodes")
.Where(d2 => d2.type = ("Der2"))
.Cast<Base>()
).ToList();
var items=xmlDoc.substands(“节点”)
.其中(d1=>d1.type==(“Der1”))
.Cast()
.Concat(xmlDoc.substands(“节点”)
.Where(d2=>d2.type=(“Der2”))
.Cast()
).ToList();
在附注中,你考虑使用而不是联合吗?Union将删除重复项,在您当前的状态下不应出现此问题。
但是,如果在某个时刻覆盖了Der1
和Der2
的相等性,则某些项可能会突然停止出现在列表中。意识到这一点很好
无论如何,我会这样做:
var items = xmlDoc.Descendants("nodes")
.Where(d1 => d1.type == ("Der1"))
.Cast<Base>()
.Concat(xmlDoc.Descendants("nodes")
.Where(d2 => d2.type = ("Der2"))
.Cast<Base>()
).ToList();
var items=xmlDoc.substands(“节点”)
.其中(d1=>d1.type==(“Der1”))
.Cast()
.Concat(xmlDoc.substands(“节点”)
.Where(d2=>d2.type=(“Der2”))
.Cast()
).ToList();
<