First of all, this article is to take care of new beginners, some places may say something basic, experts can directly ignore.
Scenario: When I used to create a website, the links in the navigation bar were all written to the page, which is not easy to maintain. So today I wrote the navigation bar configured in XML. Use the LINQ to XML.
First, we will introduce the features:
The XML file for storing navigation bar information is as follows:
- <?xml version="1.0" encoding="utf-8" ?>
- <NavigationMenu>
- <MenuItem Title="Shit" Link="/Fuck" OrderId ="2" Enabled="True" />
- <MenuItem Title="Shitter" Link="/Fucker" OrderId ="1" Enabled="True" />
- <MenuItem Title="Shitting" Link="/Fucking" OrderId ="3" Enabled="True" />
- <MenuItem Title="Shitful" Link="/Fucked" OrderId ="4" Enabled="False" />
- <MenuItem Title="Shitfully" Link="/Fuckly" OrderId ="5" Enabled="True" />
- </NavigationMenu>
Title indicates the display name, Link indicates the URL, OrderId indicates the sorting order, and Enabled indicates whether to enable the sorting. This menu will be parsed into a ul list:
- <ul>
- <li><a href="/Fucker">Shitter</a></li>
- <li><a href="/Fuck">Shit</a></li>
- <li><a href="/Fucking">Shitting</a></li>
- <li><a href="/Fuckly">Shitfully</a></li>
- </ul>
Then let's take a look at how to implement:
First, you need to create a model class that only contains attributes for the menu. Its function is to describe a specific menu item. The model passed in the system will be as follows:
- public class MenuItem
- {
- public string Title { get; set; }
- public string Link { get; set; }
- public int OrderId { get; set; }
- public bool Enabled { get; set; }
- }
Next, we will write a method to read all menu items from XML and return the List <MenuItem> generic set. In this example, all five MenuItem objects are put in List <MenuItem>.
- private List<MenuItem> GetList()
- {
- var menuPath = Server.MapPath(@"~\App_Data\Menu.xml");
- if (System.IO.File.Exists(menuPath))
- {
- XElement root = XElement.Load(menuPath);
- var menuItems = from el in root.Elements("MenuItem")
- select new MenuItem()
- {
- Title = el.Attribute("Title").Value,
- Link = el.Attribute("Link").Value,
- OrderId = int.Parse(el.Attribute("OrderId").Value),
- Enabled = bool.Parse(el.Attribute("Enabled").Value)
- };
-
- return menuItems.ToList();
- }
- else
- {
- //throw new System.IO.FileNotFoundException();
- return null;
- }
- }
In this method, I used LINQ to XML, which is the most convenient method for operating XML in. NET. However, LINQ requires that your Framework version be 3.5 or later. In this method, we first load the Menu. xml file, find all MenuItem nodes, and assign the attributes of these nodes to the new MenuItem object. When creating a MenuItem object, the syntax I use is called the object initializer. In the return statement, I wrote ToList () to ensure that the returned value is of the List <MenuItem> class type.
It is worth noting that I did not write the logic for judging Enabled and ordering OrderId in this method. Because determining whether to enable and sort the two operations is business logic and has nothing to do with data reading, we have separated the focus. GetList () is a stable method, you do not need to modify this method for any changes to your business needs, because the purpose is simple-only to read data. One method only does one thing. This is a principle.
Next, we need to write a business method, select all Enabled menus from the menu items of GetList (), and sort them in ascending order by OrderId:
- Private void FetchMenuData ()
- {
- Var menuList = GetList ();
- If (null! = MenuList & menuList. Count> 0)
- {
- // Search for all enabled menus and sort them by OrderId in ascending order
- Var menuView = (from item in menuList
- Where item. Enabled
- Orderby item. OrderId ascending
- Select item). ToList ();
-
- RptMenu. DataSource = menuView;
- RptMenu. DataBind ();
- }
- }
To be lazy, I directly bound the query results to the Repeater control. If I follow the three-tier idea, I cannot write the display in this business method. This article does not discuss layering. If you are interested, you can read the book ASP. NET design pattern.
Next, we only need to put a Repeater control on the page and call FetchMenuData () during Page_Load:
- <ul>
- <asp:Repeater ID="rptMenu" runat="server">
- <ItemTemplate>
- <li><a href="<%#Eval("Link")%>"><%#Eval("Title")%></a></li>
- </ItemTemplate>
- </asp:Repeater>
- </ul>
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- FetchMenuData();
- }
- }
There is a picture with the truth:
DEMO download and original article in http://www.wyjexplorer.cn/Home/View/B1A9770CEC6D0C4C.html