Production of ASP. NET Based on XML navigation bar

Source: Internet
Author: User

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:

 
 
  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. <NavigationMenu> 
  3.     <MenuItem Title="Shit" Link="/Fuck" OrderId ="2" Enabled="True" /> 
  4.     <MenuItem Title="Shitter" Link="/Fucker" OrderId ="1" Enabled="True" /> 
  5.     <MenuItem Title="Shitting" Link="/Fucking" OrderId ="3" Enabled="True" /> 
  6.     <MenuItem Title="Shitful" Link="/Fucked" OrderId ="4" Enabled="False" /> 
  7.     <MenuItem Title="Shitfully" Link="/Fuckly" OrderId ="5" Enabled="True" /> 
  8. </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:

 
 
  1. <ul> 
  2.     <li><a href="/Fucker">Shitter</a></li> 
  3.     <li><a href="/Fuck">Shit</a></li> 
  4.     <li><a href="/Fucking">Shitting</a></li> 
  5.     <li><a href="/Fuckly">Shitfully</a></li> 
  6. </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:

 
 
  1. public class MenuItem  
  2. {  
  3.     public string Title { get; set; }  
  4.     public string Link { get; set; }  
  5.     public int OrderId { get; set; }  
  6.     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>.

 
 
  1. private List<MenuItem> GetList()  
  2. {  
  3.     var menuPath = Server.MapPath(@"~\App_Data\Menu.xml");  
  4.     if (System.IO.File.Exists(menuPath))  
  5.     {  
  6.         XElement root = XElement.Load(menuPath);  
  7.         var menuItems = from el in root.Elements("MenuItem")  
  8.                         select new MenuItem()  
  9.                         {  
  10.                             Title = el.Attribute("Title").Value,  
  11.                             Link = el.Attribute("Link").Value,  
  12.                             OrderId = int.Parse(el.Attribute("OrderId").Value),  
  13.                             Enabled = bool.Parse(el.Attribute("Enabled").Value)  
  14.                         };  
  15.  
  16.         return menuItems.ToList();  
  17.     }  
  18.     else  
  19.     {  
  20.         //throw new System.IO.FileNotFoundException();  
  21.         return null;  
  22.     }  

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:

 
 
  1. Private void FetchMenuData ()
  2. {
  3. Var menuList = GetList ();
  4. If (null! = MenuList & menuList. Count> 0)
  5. {
  6. // Search for all enabled menus and sort them by OrderId in ascending order
  7. Var menuView = (from item in menuList
  8. Where item. Enabled
  9. Orderby item. OrderId ascending
  10. Select item). ToList ();
  11.  
  12. RptMenu. DataSource = menuView;
  13. RptMenu. DataBind ();
  14. }
  15. }

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:

 
 
  1. <ul> 
  2.     <asp:Repeater ID="rptMenu" runat="server"> 
  3.         <ItemTemplate> 
  4.             <li><a href="<%#Eval("Link")%>"><%#Eval("Title")%></a></li> 
  5.         </ItemTemplate> 
  6.     </asp:Repeater> 
  7. </ul> 
 
 
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!Page.IsPostBack)  
  4.     {  
  5.         FetchMenuData();  
  6.     }  

There is a picture with the truth:

DEMO download and original article in http://www.wyjexplorer.cn/Home/View/B1A9770CEC6D0C4C.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.