简体   繁体   中英

How to remove html characters while updating web.config?

I have a simple winforms application that allows admins to update the connection String . Admin would type in the updated connection String and it would be saved in the modified web.config file. But I am unable to remove html characters like > < that get saved while updating the connection String.

I tried HttpUlitity.HtmlDecode but that did not work out . The update code snippet looks like :

   private void updateButton_Click(object sender, EventArgs e)
        {
            var configFile = new FileInfo(@"C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb\web.config");
            var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
            var wcfm = new WebConfigurationFileMap();
            wcfm.VirtualDirectories.Add("/", vdm);
            Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
            var configSection = (ConnectionStringsSection)config.GetSection("connectionStrings");
            String connString = String.Empty;
            if (configSection != null)
            {
                var xElement = XElement.Parse(configTextBox.Text);
                connString = xElement.Attribute["connectionString"].Value;
                configSection.ConnectionStrings["ARCHQConnection"].ConnectionString = HttpUtility.HtmlDecode(connString);
                config.Save();

                //var xElement = XElement.Parse(connString);
                //connString = xElement.Attribute("connectionString").Value;
                //configSection.ConnectionStrings["ARCHQConnection"].ConnectionString = connString;
                //config.Save();
            }
            updateMessageLabel.Text = "Config Updated Successfully !! ";
            showDecryptedConfig();
        }

If the eneterd connection String is something like

then this code updates it to

<connectionStrings>
    <add name="ARCHQConnection" connectionString="&lt;connectionStrings&gt;&#xA;    &lt;add name=&quot;ARCHQConnection&quot; connectionString=&quot;LDAP://exeterblr.com/DC=exeterblr.SA,DC=in&quot; /&gt;&#xA;  &lt;/connectionStrings&gt;" />
  </connectionStrings>

even though I tried it to be updated with

<add name="ARCHQConnection" connectionString="LDAP://exeterblr.com/DC=exeterblr.SA,DC=in" />

The Logic of your code is wrong. Your code only updates the connection string attribute, but you insert the whole XML Element.

A solution would be to only insert LDAP://exeterblr.com/DC=exeterblr.SA,DC=in into your textbox.

Another solution would be to receive the connectionString attribute with this code:

var xElement = XElement.Parse(connString);
connString  = xElement.Attribute("connectionString").Value;

may be you can try this:

I have changed var to string and

HttpUtility.HtmlDecode function has been applied to next line. see if that helps you.

string connString = configTextBox.Text;
configSection.ConnectionStrings["ARCHQConnection"].ConnectionString = HttpUtility.HtmlDecode(connString);
config.Save();

Just realized that you are sending the complete XML to the Textbox and thus you need to get the value of the connectionstring attribute like this

var xElement = XElement.Parse(connString);
connString  = xElement.Attribute["connectionString"].Value;
configSection.ConnectionStrings["ARCHQConnection"].ConnectionString = connString;
config.Save();

Updated Code

XmlDocument doc = new XmlDocument();
doc.Load(configTextBox.Text); 
XmlNode ConnStr = doc.getElementByTagName("add");
connString = ConnStr.Attributes["connectionString"].Value
configSection.ConnectionStrings["ARCHQConnection"].ConnectionString = connString;
config.Save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM