Please refer this code.
Ref:
Please download the project from the above article. Add the below code in Home page.
I have Bound the DropDownList with the Pages name. I am showing the Page Description in DropDownList ddlPages.
On Selection of Page i am changing the Roles to the Selected XmlNode in the SiteMap.
HTML
<h1>
Home</h1>
Select Page
<asp:DropDownList ID="ddlPages" runat="server">
</asp:DropDownList>
Select Roles
<asp:DropDownList ID="ddlRoles" runat="server">
<asp:ListItem Text="Administrator" Value="Administrator" />
<asp:ListItem Text="User" Value="User" />
</asp:DropDownList>
<asp:Button Text="Add Roles" OnClick="AddRole" runat="server" />
<br />
<asp:Button Text="Delete Roles" OnClick="DeleteRole" runat="server" />
Namespaces
using System.Data;
using System.Xml;
C#
protected void Page_Load(object sendee, EventArgs e)
{
if (!this.IsPostBack)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~/Web.sitemap"));
this.ddlPages.DataTextField = "description";
this.ddlPages.DataValueField = "description";
this.ddlPages.DataSource = ds;
this.ddlPages.DataBind();
}
}
protected void AddRole(object sendee, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/Web.sitemap"));
XmlNodeList elementList = xmlDoc.GetElementsByTagName("siteMapNode");
foreach (XmlNode node in elementList)
{
if (node.Attributes["description"].Value == this.ddlPages.SelectedItem.Value)
{
//Set roles value here
node.Attributes["roles"].Value = this.ddlRoles.SelectedItem.Value;
}
}
xmlDoc.Save(Server.MapPath("~/Web.sitemap"));
}
protected void DeleteRole(object sendee, EventArgs e)
{
string roles = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/Web.sitemap"));
XmlNodeList elementList = xmlDoc.GetElementsByTagName("siteMapNode");
foreach (XmlNode node in elementList)
{
if (node.Attributes["description"].Value == this.ddlPages.SelectedItem.Value)
{
//Set roles value here
if (string.IsNullOrEmpty(node.Attributes["roles"].Value) && !node.Attributes["roles"].Value.Contains(this.ddlRoles.SelectedItem.Value))
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('No any roles found')", true);
}
else
{
if (node.Attributes["roles"].Value.Contains(","))
{
roles = node.Attributes["roles"].Value;
roles.Replace(this.ddlRoles.SelectedItem.Value + ",", "");
}
else
{
node.Attributes["roles"].Value = "";
xmlDoc.Save(Server.MapPath("~/Web.sitemap"));
}
}
}
}
}