Click here to Skip to main content
15,921,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to delete location path using parameter textbox ..
example : i enter name of location path in the texbox when i click button delete then location path in web config will be remove..

thanks in advance

// Op's code

C#
protected void btnsimpan_Click(object sender, EventArgs e)
{
AuthorizationRule newRule;
newRule = new AuthorizationRule(AuthorizationRuleAction.Allow);
AddRoleRule(newRule, txtllocation.Text, txtrole.Text);
}


C#
protected void AddRoleRule(AuthorizationRule newRule, string location, string role)
{
string path = Server.MapPath("~/Web.Config");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);
 
//create location element and the path attribute with it's value set to the 
//selected page 
XmlElement newLocationelement = xDoc.CreateElement("location");
XmlAttribute newLocationAttrib = xDoc.CreateAttribute("path");
newLocationAttrib.Value = location;
newLocationelement.Attributes.Append(newLocationAttrib);
XmlElement newSystemWebelement = xDoc.CreateElement("system.web");
XmlElement newAuthorizationelement = xDoc.CreateElement("authorization");
//create the allow element
XmlElement newAllowelement = xDoc.CreateElement("allow");
XmlAttribute newAllowAttrib = xDoc.CreateAttribute("users");
newRule.Roles.Add(role).ToString();
string listofRoles = "";
foreach (var item in newRule.Roles)
{
listofRoles = item.ToString();
}
newAllowAttrib.Value = listofRoles;
newAllowelement.Attributes.Append(newAllowAttrib);
//create the deny element
XmlElement newDenyelement = xDoc.CreateElement("deny");
XmlAttribute newUsersAttrib = xDoc.CreateAttribute("users");
newUsersAttrib.Value = "*";
newDenyelement.Attributes.Append(newUsersAttrib);
newAuthorizationelement.AppendChild(newAllowelement);
newAuthorizationelement.AppendChild(newDenyelement);
newLocationelement.AppendChild(newSystemWebelement);
newSystemWebelement.AppendChild(newAuthorizationelement);
xDoc.DocumentElement.AppendChild(newLocationelement);
xDoc.PreserveWhitespace = true;
//write to web.config file using xml writer
XmlTextWriter xwriter = new XmlTextWriter(path, null);
xDoc.WriteTo(xwriter);
xwriter.Close();
}


output

XML
<location path="3pm.aspx">
    <system.web>
      <authorization>
        <allow users="ADMIN" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="3pm.aspx">
    <system.web>
      <authorization>
        <allow users="COBA" />
        <deny users="*" />
      </authorization>
    </system.web>
  </location>
Posted
Updated 3-Feb-15 2:10am
v4
Comments
Sinisa Hajnal 3-Feb-15 2:20am    
Just search for it, there are plenty of examples how to change web config.
Member 11165607 3-Feb-15 3:54am    
i have tried to find but there is nothing as i want, i want dynamically delete web config.. example : if location.path = textbox1 then location path in web.config will be remove :
<location path="test">
<system.web>
<authorization>
<allow roles="USER">



please help me :(

http://stackoverflow.com/a/2260335/2745294[^]

C#
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
var section = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
section.ConnectionStrings["MyConnectionString"].ConnectionString = "Data Source=...";
configuration.Save();



C#
string path = Server.MapPath("~/Web.Config");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);

// find a node - here the one with name='abc'
XmlNode node = doc.SelectSingleNode("/location[@name='YourTextBoxName']");

// if found....
if (node != null)
{
   // get its parent node
   XmlNode parent = node.ParentNode;

   // remove the child node
   parent.RemoveChild(node);

   // verify the new XML structure
   string newXML = doc.OuterXml;

   // save to file or whatever....
   doc.Save(path);
}


Hope this helps.
 
Share this answer
 
v2
Comments
Member 11165607 3-Feb-15 3:59am    
could you give me an example to delete location path in c# ? where path= "" using parameter textbox
Sushil Mate 3-Feb-15 4:16am    
can you share your code?
Member 11165607 3-Feb-15 4:33am    
i just could to create add location path but how to delete location path ?
protected void btnsimpan_Click(object sender, EventArgs e)
{
AuthorizationRule newRule;
newRule = new AuthorizationRule(AuthorizationRuleAction.Allow);
AddRoleRule(newRule, txtllocation.Text, txtrole.Text);
}

protected void AddRoleRule(AuthorizationRule newRule, string location, string role)
{
string path = Server.MapPath("~/Web.Config");
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);

//create location element and the path attribute with it's value set to the
//selected page
XmlElement newLocationelement = xDoc.CreateElement("location");
XmlAttribute newLocationAttrib = xDoc.CreateAttribute("path");
newLocationAttrib.Value = location;
newLocationelement.Attributes.Append(newLocationAttrib);
XmlElement newSystemWebelement = xDoc.CreateElement("system.web");
XmlElement newAuthorizationelement = xDoc.CreateElement("authorization");
//create the allow element
XmlElement newAllowelement = xDoc.CreateElement("allow");
XmlAttribute newAllowAttrib = xDoc.CreateAttribute("users");
newRule.Roles.Add(role).ToString();
string listofRoles = "";
foreach (var item in newRule.Roles)
{
listofRoles = item.ToString();
}
newAllowAttrib.Value = listofRoles;
newAllowelement.Attributes.Append(newAllowAttrib);
//create the deny element
XmlElement newDenyelement = xDoc.CreateElement("deny");
XmlAttribute newUsersAttrib = xDoc.CreateAttribute("users");
newUsersAttrib.Value = "*";
newDenyelement.Attributes.Append(newUsersAttrib);
newAuthorizationelement.AppendChild(newAllowelement);
newAuthorizationelement.AppendChild(newDenyelement);
newLocationelement.AppendChild(newSystemWebelement);
newSystemWebelement.AppendChild(newAuthorizationelement);
xDoc.DocumentElement.AppendChild(newLocationelement);
xDoc.PreserveWhitespace = true;
//write to web.config file using xml writer
XmlTextWriter xwriter = new XmlTextWriter(path, null);
xDoc.WriteTo(xwriter);
xwriter.Close();
}

i want to delete location path in webconfig (using parameter textbox) below :
<location path="3pm.aspx">
<system.web>
<authorization>
<allow users="ADMIN">
<deny users="*">


Sushil Mate 3-Feb-15 4:40am    
cant you just put empty string/"" in location & save the web.config file.
Member 11165607 3-Feb-15 4:59am    
i don't understand, can you give a sample ?
i have found the solution .. thanks for your help sushil mate :)
C#
protected void DeleteRoleRule(string location)
  {
      string path = Server.MapPath("~/Web.Config");
      XmlDocument xDoc = new XmlDocument();
      xDoc.Load(path);
      XmlNodeList list = xDoc.DocumentElement.SelectNodes("location");
      if (list.Count > 0)
      {
          foreach (XmlNode node in list)
          {
              XmlAttribute attribute = node.Attributes["path"];
              if (attribute.Value == location)
              {
                  node.ParentNode.RemoveChild(node);
                  XmlTextWriter xwriter = new XmlTextWriter(path, null);
                  xDoc.WriteTo(xwriter);
                  xwriter.Close();
              }
          }
      }
  }
 
Share this answer
 
Comments
Sushil Mate 4-Feb-15 4:25am    
happy to help. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900