Now the trend of software projects is gradually becoming configuration-based development, which brings great flexibility to the project, especially after the emergence of XML, configuration-based development becomes very important. In order to enable dynamic configuration of the page menu in the project, I did not save the data in the database, but wrote it in an XML file, the job of the program is to dynamically read and generate navigation menus based on the content of the XML file. (It is actually parsing XML files). I will share the content for your reference and give more comments.
The XML file format is as follows:
<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<Node name = "Project List entry" url = "projectlisttest. aspx"/>
<Node name = "Project Manager assignment" url = "pmprojectlisttest. aspx"/>
<Node name = "project information maintenance" url = "projectinfomaintaintest. aspx"/>
<Node name = "Confirm project information" url = "projectconfirmtest. aspx"/>
<Node name = "exit" url = "logintest. aspx"> </node>
</Root>
The following task is to dynamically generate a menu based on the configuration.
Since it is easy to implement, I don't need to describe it much. The code implementation is shown below:
Public void render ()
{
Stringbuilder sb = new stringbuilder ();
List <navigator> List = new list <navigator> ();
Loadfromxml (list );
Foreach (navigator in List)
{
SB. appendformat (@ "<a href = '{0}'> {1} </a> |", navigator. url, navigator. Name );
}
This. ltlnavigator. Text = sb. tostring (). Remove (sb. Length-1 );
}
Private void loadfromxml (list <navigator> List)
{
String Path =
Httpcontext. Current. server. mappath (
Urlhelper. getclienturl (configurationmanager. receivettings ["headnavigatorfilepath"]);
Xmldocument Doc = new xmldocument ();
Doc. Load (PATH );
Xmlnodelist nodes = Doc. selectnodes ("/root /*");
Foreach (xmlnode node in nodes)
{
Navigator navigator = new Navigator ();
Navigator. Name = node. attributes ["name"]. innertext;
Navigator. url = node. attributes ["url"]. innertext;
List. Add (Navigator );
}
}
The navigator class is as follows:
Protected class Navigator
{
Private field # region private field
Private string m_name;
Private string m_url;
# Endregion
Pubilc property # region pubilc Property
Public string name
{
Get
{
Return m_name;
}
Set
{
M_name = value;
}
}
Public String URL
{
Get
{
Return m_url;
}
Set
{
M_url = value;
}
}
# Endregion
}
}
Generated: