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..

enter image description here

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");

New output:

enter image description here

MekeniKine
  • 285
  • 1
  • 6
  • 13

1 Answers1

3

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.

Input "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();

Output 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>
  • wow thanks, i'll try this now, can I use my xroot variable directly right? – MekeniKine Feb 10 '13 at 04:21
  • 1
    @MekeniKine Perhaps. The property names will need to be modified, at the least. –  Feb 10 '13 at 04:23
  • Great! it really works! thank you so much sir question update see new xml output! :) – MekeniKine Feb 10 '13 at 04:36
  • Sir if it is okay with you got 1 more simple question in GroupName is inline with Menu can I do Menu will be under GroupName? – MekeniKine Feb 10 '13 at 04:39
  • @MekeniKine Just put it there to begin with. `xnavigations.Add(xmenus);` shlj –  Feb 10 '13 at 05:59
  • 1
    @MekeniKine Just put it there to begin with: `xnavigations.Add(xmenus)` should be `xgroupname.Add(xmenus)`, where `xgroupname` refers the correct GroupName element. –  Feb 10 '13 at 06:00
  • Got confused I have no xgroupname :# – MekeniKine Feb 10 '13 at 07:21