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!