使用(LINQ to XML)获取叶节点的父节点或映射到更好的类? [英] Get Parents of leaf nodes using (LINQ to XML) or mapping to better classes?

查看:43
本文介绍了使用(LINQ to XML)获取叶节点的父节点或映射到更好的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从这个深度嵌套的XML数据中可视化树,但很困惑。我应该将XML对象映射到我自己的类(将数据结构简化为带链接的节点(如果节点有子节点,则简化为子节点),或者只使用LINQ直接查询?



意识到这对许多人来说可能看起来很简单,但现在已经花费了近2天的时间,所以会很感激某些方向。 



这是XML数据注意每个< Clade>元素具有实际属性的子元素(例如"name","branch_length"等)以及在某些情况下的子< clade>元素:

Am trying to visualize a tree from this deeply nested XML data but confused. Should I be mapping the XML objects to my own class (that simplifies the data structure into nodes with links (to children if the node has them), or just query direct using LINQ?

Realize this may seem very simple for many but have spent almost 2 days on it now so would appreciate some direction. 

Here is the XML data. Note each <Clade> element has child elements that are really attributes (eg "name", "branch_length", etc) as well as in some cases, child <clade> elements:

<clade> <name>Life on Earth</name> <branch_length>1.0</branch_length> <tol>1</tol> <age_mya>3000.167</age_mya> <orig_branch_length>1.0</orig_branch_length> <clade> <name>unnamed node</name> <branch_length>0.3888888888888889</branch_length> <age_mya>2999.778</age_mya> <pw_index>1</pw_index> <orig_branch_length>0.3888888888888889</orig_branch_length> <clade> <name>Eukaryotes</name> <branch_length>798.7777777777778</branch_length> <tol>3</tol> <age_mya>2201.0</age_mya> <pw_index>2</pw_index> <orig_branch_length>798.7777777777778</orig_branch_length> <clade> <name>unnamed node</name> <branch_length>1.6666666666666667</branch_length> <age_mya>2199.333</age_mya> <pw_index>1</pw_index> <orig_branch_length>1.6666666666666667</orig_branch_length> <clade> <name>Opisthokonts</name> <branch_length>579.3333333333335</branch_length> <tol>2372</tol> <age_mya>1620.0</age_mya> <pw_index>1</pw_index> <orig_branch_length>579.3333333333335</orig_branch_length> <clade> <name>Animals</name> <branch_length>870.0</branch_length> <tol>2374</tol> <age_mya>750.0</age_mya> <pw_index>1</pw_index> <orig_branch_length>870.0</orig_branch_length> <clade> <name>Bilateria</name> ... etc

要绘制树形图,我需要获取所有叶子对象(即物种)并处理树。但是,此查询不会返回子< clade>对象/种类,而不是它们的属性(由于数据结构):

To graph a tree, i need to get all the leaf objects (i.e. species) and work up the tree. However, this query doesn't return child <clade> objects / species, but rather their attributes (because of the data structure):

XDocument xmlTree = XDocument.Load(Application.streamingAssetsPath + "/tree.xml");
foreach (XElement childElement in xmlTree.Descendants().Where(d => !d.Elements().Any())) 
{
Console.WriteLine(childElement);

返回

Returns

<name>Banana</name>              
<branch_length>304.8128342245987</branch_length>
<tol>21506</tol>
<age_mya>0.0</age_mya>
<pw_index>1</pw_index>                     
...etc for some 4000 lines

LINQ是否有办法以某种方式理解< Clade>是节点并附加子元素< name>,< branch_length>等等 - 或者我是否需要直接映射到班级?
b
$
如果是后者 - 我如何在结合儿童和家长关系的同时做到这一点?

Is there a way for LINQ to somehow understand that the <Clade> is the node and attach subelements <name>, <branch_length> etc to it – or do I need to map directly to classes?

If the latter – how can i do this whilst incorporating the child and parent relationships?

到目前为止,我有:

using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;

public class PlotTree : MonoBehaviour 
{
    public Node nodePrefab; //represent each species 
    public float scale = 0.1f;
    public float ySpacing = 0.1f; // spacing

    void Start()
    {
        // load XML file into memory
        XDocument xmlTree = XDocument.Load(Application.streamingAssetsPath + "/tree.xml");

        // create and populate list of nodes/species/clades from XDocument 
        // BUT HOW TO ADD LISTS (not sure...)
        IEnumerable<Clade> clades = from n in xmlTree.Descendants("clade")
                                    select new Clade(
                                                         (string)n.Element("name"),
                                                         (float)n.Element("branch_length"),
                                                         (float)n.Element("age_mya"),
                                                         (float)n.Element("orig_branch_length"));
    }
    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 List<Clade> children { get; set; }

        public Clade(string n, float b, float a, float o) //List<Clade> c
        {
            name = n;
            branch_length = b;
            age_mya = a;
            orig_branch_length = o;
            //children = c;
        }
    }

当然这还不够,因为它不允许我查询没有孩子的所有物种......


非常感谢任何能指引我正确方向的慷慨的人。 

Of course this is insufficient because it doesn't let me query all species without children...
Many thanks to any generous person who can point me in the right direction. 

最多为

推荐答案

您好MaxXR,

Hi MaxXR,

感谢您在此发帖。

对于您的问题,您的意思是你吗?想要获取xml节点并像树一样显示它?如果是,您可以尝试以下代码。

For your question, do you mean you want to get the xml nodes and show it like a tree? If yes, you could try the code below.

XDocument doc = XDocument.Load(@"XML.xml");
string tree = doc.Root.DescendantsAndSelf().Aggregate("", (bc, n) => bc + n.Ancestors().Aggregate("", (ac, m) => (m.ElementsAfterSelf().Any() ? "| " : "  ") + ac, ac => ac + (n.ElementsAfterSelf().Any() ? "+-" : "\\-")) + n.Name + "\n");
            

我使用一个简单的xml文件作为参考。

I use a simple xml file for reference.

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <Brand name="Brand1">
    <product name="Product1" />
    <product name="Product2" />
  </Brand>
  <Brand name="Brand2">
    <product name="Product3" />
    <product name="Product4" />
  </Brand>
</root>




如果我误解了你想要的东西,请随时与我们联系。

If I misunderstand what you want, please feel free to contact us.

最好的问候,

Wendy


这篇关于使用(LINQ to XML)获取叶节点的父节点或映射到更好的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆