重组XML树基于与&QUOT元素序列ID"和"父ID" [英] Restructure an XML tree based on sequence of elements with "ID" and "parent ID"

查看:176
本文介绍了重组XML树基于与&QUOT元素序列ID"和"父ID"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望发生的是,我希望把其ID等于PARENTID元素里面呢?因此,像在我的例子,其的ParentId = 1应该是一个ID = 1我怎么可能做到这一点组内的集团?有这么糊涂..

What I want to happen is that I want to put inside the element whose ID is equal to ParentID? So like in my example the Group whose ParentId=1 should be inside the Group whose Id=1 How can I possibly do this? Got so confused..

截至目前,这里是我的代码:

As of now here is my code:

XElement xroot = new XElement("Root");
        XElement xnavigations = null;
        XElement xmenus = null;

        foreach (DataRow navigations in GetNavigationSets().Rows)
        {
            xnavigations = new XElement("Group", 
                new XElement("GroupName", navigations["name"].ToString())
                );
            xnavigations.SetAttributeValue("Id", navigations["id"].ToString());
            xnavigations.SetAttributeValue("ParentId", navigations["parent_id"].ToString());

            foreach (DataRow menus in GetMenusInNavigationSetByNavigation(int.Parse(navigations["id"].ToString())).Rows)
            {
                foreach (DataRow menu in GetMenuById(int.Parse(menus["menu_id"].ToString())).Rows)
                {
                    xmenus = new XElement("Menu", 
                        new XElement("Name", menu["name"].ToString()),
                        new XElement("Price", menu["price"].ToString()),
                        new XElement("Description", menu["description"].ToString())
                        );

                    xnavigations.Add(xmenus);
                }
            }

            xroot.Add(xnavigations);
        }

        xroot.Save("main.xml");

新的输出:

推荐答案

下面是一个变异的方法,它的后的副作用依赖的。它并不像递归和重楼干净,但它往往是足够的。而且,这是相当不错的容易写

Here is a mutating approach and it relies upon side-effects. It's not as clean as recursion and re-building, but it is often "sufficient". And, it's pretty darn easy to write.

输入XML:

var root = XElement.Parse(@"<root>
<group id='1' />
<group id='4' parent='2' />
<group id='2' parent='1' />
<group id='3' parent='2' />
<group id='5' />
</root>");



拐进树:

Turn into tree:

// So we can find parent by ID
var groupMap = root.Elements("group")
  .ToDictionary(e => (string)e.Attribute("id"), e => e);

// ToList so we don't iterate modified collection
foreach (var e in root.Elements().ToList()) {
  XElement parent;
  if (groupMap.TryGetValue((string)e.Attribute("parent") ?? "", out parent)) {
     // Unlike standard XML DOM,
     // make sure to remove XElement from parent first
     e.Remove();
     // Add to correct parent
     parent.Add(e);
  }
}

// LINQPad :-)
// root.Dump();

输出XML:

<root>
  <group id="1">
    <group id="2" parent="1">
      <group id="4" parent="2" />
      <group id="3" parent="2" />
    </group>
  </group>
  <group id="5" />
</root>

这篇关于重组XML树基于与&QUOT元素序列ID&QUOT;和&QUOT;父ID&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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