Click here to Skip to main content
11,577,643 members (21,824 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
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
      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 10-Dec-12 20:39pm
Edited 10-Dec-12 20:55pm
v2
Comments
Programm3r at 11-Dec-12 2:43am
   
Hi - Could you please post some code, to demonstrate what you have done so far? Many thanks!
kkakadiya at 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 at 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.
kkakadiya at 11-Dec-12 3:22am
   
ok so text.txt file at d:websites2 location contains div tag. it shuold be deleted.right????
NNos2012 at 11-Dec-12 3:23am
   
yes , exactly
kkakadiya at 11-Dec-12 3:25am
   
ok let me check what i can do for you...but can u tell me or paste your code here what you had done so far....so i get to know..thanks
NNos2012 at 11-Dec-12 3:32am
   
the code i wrote is above and about deleting from the file, i have still searching for it , so if you can help me, i would be in pleasure for you
thanks
kkakadiya at 11-Dec-12 3:34am
   
ok wait...:)
kkakadiya at 11-Dec-12 3:38am
   
please paste your both .aspx and .aspx.cs code here...
NNos2012 at 11-Dec-12 3:43am
   
which code you want else .. that is all
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
v2
Comments
vishal jodh at 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)

  Print Answers RSS
0 DamithSL 180
1 OriginalGriff 115
2 Richard MacCutchan 100
3 Abhinav S 90
4 Peter Leow 80
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 681
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 375


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 11 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100