Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i have text file saving html tags in it to draw these tags later , i want to check if the file contains a div with a specific id , delete it with its content.

can any one help me in this , please...

my code which writes in text file is below
C#
string aa = GetWebPageTitle("http://localhost:3135/WebSite2/Default4.aspx");
  string appPath = @"D:\WebSite2\";
  string filePath = appPath + "Text.txt";
  StreamWriter w;
  w = File.CreateText(filePath);
  w.WriteLine(aa);
  w.Flush();
  w.Close();
  mydiv.InnerText = "File created and write successfully!<br />";
  mydiv.InnerText += filePath;
  mydiv.InnerHtml = aa;


thanks
Posted
Updated 10-Dec-12 20:55pm
v2
Comments
Programm3r 11-Dec-12 2:43am    
Hi - Could you please post some code, to demonstrate what you have done so far? Many thanks!
kk2014 11-Dec-12 3:04am    
you mean to say the code above u given will write in text file and delete mydiv lines from that....right?
NNos2012 11-Dec-12 3:17am    
No, mydiv is a div which i draw text file content in it.
now, i want to delete a div inside this file according to its id.
kk2014 11-Dec-12 3:22am    
ok so text.txt file at d:websites2 location contains div tag. it shuold be deleted.right????
NNos2012 11-Dec-12 3:23am    
yes , exactly

Hi NNos2012,

You can get the div tag and can replace the tag in code so that that div will not be visible on front end. I have some code you can try them out...

C#
string replaceString = string.Join(";",
                           File.ReadAllLines("<div id="id1">")
                           ).Replace("@", "<div id="id1" style="display: none;">");

   File.WriteAllText("<div id="id1">", replaceString);
   Console.WriteLine("Done");
   Console.ReadLine();</div></div></div>


Or try the following code:

// Open a file for reading
StreamReader streamReader
streamReader = File.OpenText(fileName)
// Now, read the entire file into a strin
string contents = streamReader.ReadToEnd();
streamReader.Close()

// Write the modification into the same fil
StreamWriter streamWriter=File.CreateText(fileName);

streamWriter.Write(contents.Replace("String1", "String2"));
streamWriter.Close();


Links:
1. Link-1
2. Link-2
3. Link-3

Thanks
 
Share this answer
 
Here I am developed Win Form application To delete Div Section from Text file
in my form I have taken Text Box and Button

on click button event
private void button1_Click(object sender, EventArgs e)
        {
            string search = "<div id=\"" + textBox1.Text + "\">";
            List<string> lines = new List<string>(File.ReadAllLines("Your File Path"));
            foreach (string line in lines)
            {
                line.TrimStart(); line.TrimEnd();
                if (line.Contains(search))
                {
                    SLineNumber = i;
                }
                if (SLineNumber > 0)
                {
                    count++;
                    if (line.Contains("<div id=") && !line.Contains(search) || line.Contains("<div>"))
                    {
                        innerDiv++;
                    }
                }
                if (line.Contains("</div>") && innerDiv == 0 & SLineNumber != 0)
                {
                    ELineNumber = i;
                    break;
                }
                else if (line.Contains("</div>") && SLineNumber != 0)
                {
                    innerDiv--;
                }
                i++;
            }
            lblSLine.Text = SLineNumber.ToString();
            lblELine.Text = ELineNumber.ToString();
            if (SLineNumber != 0 && ELineNumber != 0)
            {
                 lines.RemoveRange(SLineNumber,ELineNumber-SLineNumber);
                 File.WriteAllLines("Your file Path", lines.ToArray());
                 MessageBox.Show(" Remove the Section of Div " + textBox1.Text);
            }
            else
            {
                MessageBox.Show("Div  id=" + textBox1.Text + "  does not exist");
            }
        }



This Code Delete Div Section of specific Id


I hope this will be help full for you.
 
Share this answer
 
v2
Comments
vishal jodh 11-Dec-12 7:48am    
if my code will match u r requirement the pls Rate this Solution

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