tags:

views:

121

answers:

2

Hello,

I am looking to create a dynamic XML navigation menu, which is made of MenuItem's and SubMenuItem's (both classes). I want to parse the following XML and use LINQ to parse it and retrieve the data.

Here is a sample XML:

<?xml version="1.0" encoding="utf-8" ?>

<MenuItems>
  <Item Id="1" Url="Default.aspx" LinkText="Home" Description="Test Description" Target="">
    <SubItem Id="1" ParentId="1" Url="Default.aspx" LinkText="SubMenu1" Description="Test Description" Target="" />
    <SubItem Id="2" ParentId="1" Url="Default.aspx" LinkText="SubMenu2" Description="Test Description" Target="" />
    <SubItem Id="3" ParentId="1" Url="Default.aspx" LinkText="SubMenu3" Description="Test Description" Target="" />
  </Item>
  <Item Id="2" Url="Default2.aspx" LinkText="Menu2" Description="Test Description" Target="" />
  <Item Id="3" Url="Default3.aspx" LinkText="Menu3" Description="Test Description" Target="" />
</MenuItems>

I want to use LINQ, but am having trouble with the syntax. Can you use nested LINQ queries to populate two object lists as below?

List<MenuItem> MenuItems = null;
try
{
    XElement xmlDoc = XElement.Load(xmlPath + xmlFileName);
    if (xmlDoc != null)
    {
        MenuItems =
                (from menuItem in xmlDoc.Descendants("Item")
                select new MenuItem
                {
                    Id = menuItem.Attribute("Id").Value,
                    Description = menuItem.Attribute("Description").Value,
                    LinkText = menuItem.Attribute("LinkText").Value,
                    Url = menuItem.Attribute("Url").Value,
                    Target = menuItem.Attribute("Description").Value,
                    SubMenuItems = (from subMenuItem in xmlDoc.Descendants("SubItem")
                                    select new SubMenuItem
                                    {
                                        Id = menuItem.Attribute("Id").Value,
                                        ParentId = menuItem.Attribute("ParentId").Value,
                                        Description = menuItem.Attribute("Description").Value,
                                        LinkText = menuItem.Attribute("LinkText").Value,
                                        Url = menuItem.Attribute("Url").Value,
                                        Target = menuItem.Attribute("Description").Value,
                                    }).ToList<SubMenuItem>(),
                }).ToList<MenuItem>();
    }
}

Can someone help with the LINQ to parse this XML into these two classes, which are nested within one another?

public class MenuItem
{
    public MenuItem() { }

    private string id;
    private string url;
    private string linkText;
    private string description;
    private string target;
    private List<SubMenuItem> subMenuItems = new List<SubMenuItem>();

    public string Id { get { return id; } set { id = value; } }
    public string Url { get { return url; } set { url = value; } }
    public string LinkText { get { return linkText; } set { linkText = value; } }
    public string Description { get { return description; } set { description = value; } }
    public string Target { get { return target; } set { target = value; } }
    public List<SubMenuItem> SubMenuItems { get { return subMenuItems; } set { subMenuItems = value; } }
}

public class SubMenuItem
{
    public SubMenuItem() { }

    private string id;
    private string parentid;
    private string url;
    private string linkText;
    private string description;
    private string target;

    public string Id { get { return id; } set { id = value; } }
    public string ParentId { get { return parentid; } set { parentid = value; } }
    public string Url { get { return url; } set { url = value; } }
    public string LinkText { get { return linkText; } set { linkText = value; } }
    public string Description { get { return description; } set { description = value; } }
    public string Target { get { return target; } set { target = value; } }
}

Thank you!

+1  A: 

I think you could solve this with a bit of recursion. You could probably have just MenuItem instead of both classes. They are basically same. Then, if you create a SelectDecendants method, it would keep calling itself to get the children of the current node. Something like this:

class Program
{
 static void Main(string[] args)
 {
  var doc = XDocument.Load("input.xml");
  var menuItems = SelectDescendants(doc.Elements("MenuItems").Elements());
 }

 public static List<MenuItem> SelectDescendants(IEnumerable<XElement> menuItems)
 {
  return (from menuItem in menuItems
     select new MenuItem
     {
      Id = menuItem.Attribute("Id").Value,
      Description = menuItem.Attribute("Description").Value,
      LinkText = menuItem.Attribute("LinkText").Value,
      Url = menuItem.Attribute("Url").Value,
      Target = menuItem.Attribute("Description").Value,
      SubMenuItems = SelectDescendants(menuItem.Elements()).ToList()
     }).ToList();
 }

}
Jake Pearson
I am having some trouble getting the syntax of this to work. The linq query doesn't seem to understand the syntax. Can you clarify at all how I use the linq directly with the XElement menuItem?
kazzamalla
Here you go, I coded it off the top of my head. The code above has now been tested. Hope it helps.
Jake Pearson
Thank you! I got a modified version of this to work before you edited with the tested code, and then used the tested code you passed for reference. Thanks a lot for your time!
kazzamalla
+2  A: 

It might help if you isolate your logic a bit more. Best bet, IMO, would be to add a constructor to both MenuItem and SubMenuItem that accepts an XElement.

public class MenuItem
{
    public MenuItem() { }

    public MenuItem(XElement xmlDoc)
    {
        this.id = xmlDoc.Element("Id").Value;
        this.url = xmlDoc.Element("Url").Value;
        // etc.
        subMenuItems = (from subMenuItem in xmlDoc.Descendants("SubItem")
                        select new SubMenuItem(subMenuItem)).ToList();

    // etc.
}

public class SubMenuItem
{
    public SubMenuItem() { }

    public SubMenuItem(XElement xmlDoc)
    {
        this.id = xmlDoc.Element("Id").Value;
        // etc.
    }
    // etc.
}

That'd clear up your initial select to be:

MenuItems = (from menuItem in xmlDoc.Descendants("Item")
             select new MenuItem(menuItem)).ToList();
Jacob Proffitt
Thanks so much for your help. I tried it both ways and will use this solution in the future, I just had already started on Jakers way so I went with that one. Thanks again!
kazzamalla