Checking For Bad/ Abuse Words On Form Posting Using An XML File In C#

Step 1: Create a aspx page “Bad-words-check.aspx” as below.

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Bad-words-check.aspx.cs"  
  2.   
  3. Inherits="ResponsiveForms_ASP_NET_Bad_words_Bad_words_check" %>  
  4.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  5.     <html xmlns="http://www.w3.org/1999/xhtml">  
  6.   
  7.     <head runat="server">  
  8.         <title></title>  
  9.         <style>  
  10.             body {  
  11.                 font-family: 'Arial';  
  12.             }  
  13.         </style>  
  14.     </head>  
  15.   
  16.     <body>  
  17.         <form id="form1" runat="server">  
  18.             <div> <textarea runat="server" id="txtmsg" class="txtarea Txtbox" rows="6" cols="50" style="resize: none;  
  19.   
  20. width: 300px; height: 120px;"></textarea> <br /><br />  
  21.                 <asp:Button ID="btnpost" ClientIDMode="Static" runat="server" Text="SUBMIT" Width="100px" OnClick="btnpost_Click" /> <br /><br />  
  22.                 <asp:Label ID="lblmsg" runat="server" style="color:Red;font-weight:bold;"></asp:Label>  
  23.             </div>  
  24.         </form>  
  25.     </body>  
  26.   
  27.     </html>   

Step 2: Make cs file as below,

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Web.UI.WebControls;  
  6. using System.Xml;  
  7. public partial class ResponsiveForms_ASP_NET_Bad_words_Bad_words_check: System.Web.UI.Page {  
  8.     protected void Page_Load(object sender, EventArgs e) {}  
  9.     protected void btnpost_Click(object sender, EventArgs e) {  
  10.         try {  
  11.             XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object  
  12.             xmlDoc.Load(Server.MapPath("../WordList.xml")); // Load the XML document from the specified file  
  13.             // Get elements  
  14.             XmlNodeList badwords = xmlDoc.GetElementsByTagName("word");  
  15.             for (int i = 0; i < badwords.Count; i++) {  
  16.                 if (txtmsg.Value.ToUpperInvariant().Contains(badwords[i].InnerText.ToUpperInvariant())) {  
  17.                     lblmsg.Text = "Your text contains bad/abuse words!";  
  18.                     break;  
  19.                 } else lblmsg.Text = "Your text is clean!";  
  20.             }  
  21.         } catch (Exception ex) {}  
  22.     }  
  23. }  

Step 3: Add xml file “WordList.xml” to your project,

Now you can run the page and see the result. If you have any further words to be restricted then you can add it manually to the xml file.

The pages and xml file are attached.