问题描述
已将XML树数据映射到包含"Children"和"Parent"节点的子类的节点类(称为"Clade")。 Children类可以工作,但是'Parents'不可以。 尝试使用.Ancestors方法(参见第二个循环)但是它返回
所有祖先(不是节点的直接父级)。使用.Parent也不像.Children那样工作。
$
有没有办法1)在我映射类时以某种方式获得真正的分支父母2)以某种方式从叶子列表中查询/获取父节点(我的名字叫做leafNodes)?
$
我的代码如下 - 已插入(&//`PROBLEM" in案例有用。
Have mapped XML tree data to a node class (called "Clade") containing subclasses for 'Children' and 'Parent' nodes. The Children class works, but the 'Parents' one doesn't. Tried to use the .Ancestors method (see 2nd loop) but it returns all ancestors (not the node's direct parent). using .Parent also does't work like .Children.
Is there a way to 1) somehow get the true Parents of the Clade when I map classes OR 2) somehow query/get the parent nodes from a leaf list (mine is called leafNodes)?
My code below – have inserted ("//PROBLEM" in case helpful.
void Start()
{
// load XML file into memory
XDocument xmlTree = XDocument.Load(Application.streamingAssetsPath + "/tree.xml");
//gen class for nodes/species/clades from XDocument
IEnumerable<Clade> clades = from n in xmlTree.Descendants("clade")
select new Clade()
{
Name = (string)n.Element("name"),
Branch_length = (float)n.Element("age_mya"),
Orig_branch_length = (float)n.Element("orig_branch_length"),
Children = from f in n.Elements("clade")
select new Child()
{
Name = (string)f.Attribute("name"),
},
Parents = from z in n.Ancestors("clade") //PROBLEM HERE?
select new ParentNode()
{
Name = (string)z.Attribute("name"),
}};
//get leafs
IEnumerable<Clade> leafNodes = from Clade n in clades
where n.Children.Count() == 0
select n;
//PROBLEM HERE: How to get the parent(s) of leaf nodes into a collection (so i can give them coordinates)?
IEnumerable<Clade> parentsofLeafNodes = from Clade z in leafNodes
select z.Parents;
}
class Clade
{
public string Name { get; set; }
public float Branch_length { get; set; }
public float Age_mya { get; set; }
public float Orig_branch_length { get; set; }
public IEnumerable<Child> Children { get; set; }
public IEnumerable<ParentNode> Parents { get; set; }
}
class Child
{
public string Name { get; set; }
}
class ParentNode
{
public string Name { get; set; }
}
数据是' tree.xml '深度嵌套物种的文件(下面一些子节点的样本):
Data is this 'tree.xml' file of deeply nested species (sample of some child nodes below):
<name>Hominids</name>
<branch_length>72.1099999999996</branch_length>
<tol>16299</tol>
<age_mya>19.73</age_mya>
<pw_index>1</pw_index>
<orig_branch_length>72.1099999999996</orig_branch_length>
<clade>
<name>unnamed node</name>
<branch_length>15.158333333333758</branch_length>
<tol>16416</tol>
<age_mya>4.572</age_mya>
<pw_index>2</pw_index>
<orig_branch_length>15.158333333333758</orig_branch_length>
<clade>
<name>Homo</name>
<branch_length>2.0953472222222445</branch_length>
<tol>16418</tol>
<age_mya>2.476</age_mya>
<pw_index>2</pw_index>
<orig_branch_length>2.0953472222222445</orig_branch_length>
<clade>
<name>Homo erectus</name>
<branch_length>1.4763194444426517</branch_length>
<tol>9999992</tol>
<age_mya>1.0</age_mya>
<orig_branch_length>1.4763194444426517</orig_branch_length>
</clade>
<clade>
<name>Modern human</name>
如果您知道如何解决此问题/获取父母,请与我们联系。
谢谢你
Please let me know if you know how to solve this / get parents.
Thankyou
推荐答案
class Clade
{
public string Name { get; set; }
public float Branch_length { get; set; }
. . .
public Clade Parent { get; set; }
public IEnumerable<Clade> Children { get; set; }
}
. . .
static Clade ProcessCladeElement( XElement e, Clade parent )
{
Clade clade = new Clade();
clade.Name = e.Element( "name" ).Value;
clade.Branch_length = float.Parse( e.Element( "branch_length" ).Value );
clade.Parent = parent;
clade.Children = e.Elements( "clade" ).Select( c => ProcessCladeElement( c, clade ) ).ToList();
return clade;
}
. . .
XDocument xmlTree = XDocument.Load( ... );
Clade root_clade = ProcessCladeElement( xmlTree.Root.Element( "phylogeny" ).Element( "clade" ), null );
这篇关于是否有可能只获得父母而不是所有祖先? (自下而上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!