通过xml操作网页

通过xml的设置可以进行页面的设置,以及网页之间的跳转。

此例为把xml中的数据读出,然后绑定到控件上,可以根据读出内容的URL进行页面之间的跳转。

1,首先需要进行xml的编写。例如:

Menu.xml --->

<?xml version="1.0" encoding="utf-8" ?> <Menu>     <!--<TopMenu>     <MenuItem Name="ListAirLine" Description="基础管理" Url="/BackStage/BasicManagement/Management.aspx" />     <MenuItem Name="ListOrders" Description="订单管理" Url="/BackStage/OrderManagement.html"/>     <MenuItem Name="ListInternalUser" Description="后台用户管理" Url="/BackStage/InternalUserManagement/Management.aspx"/>   </TopMenu>-->

    <TopMenu Name="Basedata" Description="基础管理" Url="/BackStage/BasicManagement/Management.aspx">         <SubTitle Title="基础管理">             <LeftMenuItem Name="Basedata_AirLine" Description="国内航线" Url="/BackStage/BasicManagement/Management.aspx?Type=Airline" />             <LeftMenuItem Name="Basedata_AirlineInt" Description="国际航线" Url="/BackStage/BasicManagement/Management.aspx?Type=AirlineInt" />             <LeftMenuItem Name="Basedata_AirCompany" Description="航空公司" Url="/BackStage/BasicManagement/Management.aspx?Type=AirCompany" />             <LeftMenuItem Name="Basedata_Airport" Description="机场管理" Url="/BackStage/BasicManagement/Management.aspx?Type=Airport" />             <LeftMenuItem Name="Basedata_Plane" Description="飞机信息" Url="/BackStage/BasicManagement/Management.aspx?Type=Plane" />             <LeftMenuItem Name="Basedata_FavorType" Description="优惠信息" Url="/BackStage/BasicManagement/Management.aspx?Type=FavorType" />         </SubTitle>     </TopMenu></Menu>

 

2.需要把编写好的Menu.xml放到web.config中以便于对其的调用

  <appSettings>
    <add key="MenuConfig" value="/Configuration/Menu/Menu.xml" />

</appSettings>

 

3.可以通过ConfigurationManager来获取Menu.xml文件

 

public static IEnumerable<dynamic> GetMainMenus()
        {
            string path = ConfigurationManager.AppSettings["MenuConfig"];
            path = HttpContext.Current.Server.MapPath(path);
            var xElement = XElement.Load(path).Elements("TopMenu");
            var xElements = xElement as IList<XElement> ?? xElement.ToList();
            if (xElements.Any())
            {
                var menuConfig = from p in xElements
                                 select p;
                var menus = new List<dynamic>();
                foreach (var menu in menuConfig)
                {
                    menus.Add(new { Name = menu.FirstAttribute.Value, Description = menu.Attribute("Description").Value, Url = menu.Attribute("Url").Value });
                }
                return menus;
            }
            return null;
        }

4.把读取出的数据绑定到相应的控件上

MenuList.DataSource = menus;
                MenuList.DataBind();

因为绑定的listView 中有<a href> 属性,只要给定相应绑定值的url则会直接跳转。代码如下:

 

 <asp:ListView runat="server" ID="MenuList">
                        <ItemTemplate>
                            <li id=‘<%#Eval("Name") %>‘><a href=‘<%#Eval("Url") %>‘>
                                <%#Eval("Description") %></a> </li>
                        </ItemTemplate>
                    </asp:ListView>

 

PS: <%#Eval()%> 为到达绑定字段的值

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。