从文本文件中删除div [英] Deleting div from text file

查看:79
本文介绍了从文本文件中删除div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

i有文本文件保存html标签以便稍后绘制这些标签,我想检查文件是否包含具有特定ID的div,删除它的内容。



任何人都可以帮助我,请...



我在文本文件中写的代码是下面

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

推荐答案

您好NNos2012,



您可以获取div标签并可以替换代码中的标签,以便在前端看不到该div。我有一些代码你可以尝试一下......



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();





链接:

1. Link-1

2. Link-2

3. Link-3



谢谢



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

Thanks


这里我开发了Win Form应用程序要从文本文件中删除Div Section

在我的表单中我已经拍摄文本框和按钮



点击按钮事件

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");
            }
        }







此代码删除特定ID的Div部分< br $> b $ b



我希望这对您有所帮助。




This Code Delete Div Section of specific Id


I hope this will be help full for you.


这篇关于从文本文件中删除div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆