我有这样的数据
*******************[MESSAGES OF MASTER JOB]*******************<BR><BR><BR><BR>
CA ARCserve Backup -- Backup<BR><BR><BR><BR>
[SubJob]<BR><BR><BR><BR>
Totals For................... SubJob 1<BR><BR>
Job No....................... 18<BR><BR>
Job ID....................... 8786<BR><BR>
Total Session(s)............. 8<BR><BR>
Total Size (Disk)............ 512.12 GB<BR><BR>
Total Size (Media)........... 514.29 GB<BR><BR>
Elapsed Time................. 4h 30m 54s<BR><BR>
Average Throughput........... 1.91 GB/min<BR><BR>
SubJob Status................ Finished<BR><BR><BR><BR>
该数据来自HTML,我在文本文件中打开..我的问题是我想删除
将其替换为新行,以便我可以在记事本中读取该数据,就像我在网上读取该数据一样我的代码不能正常工作..如何更换它们?
这是我的代码
OpenFileDialog ofd = new OpenFileDialog();
string x;
string[] wards = {"<BR>", "<br>"};
if (ofd.ShowDialog() == DialogResult.OK)
{
StreamReader read = new StreamReader(ofd.FileName);
using(System.IO.StreamReader txttemp = read)
{
x = txttemp.ReadLine();
foreach (string ward in wards)
{
if (x.Contains("<br>")|| x.Contains("<BR>"))
{
ward.Replace("<BR>", Environment.NewLine);
}
}
}
}
答案 0 :(得分:0)
首先,当你使用Replace()时,它返回一个修改后的字符串,而不是只编辑目标,所以它应该是
ward = ward.Replace("<BR>", Environment.NewLine);
但是,您不应该在循环期间编辑元素。 所以我的建议是
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
string[] yourHtmlInRows = File.ReadAllLines(ofd.FileName);
List<string> resultYouWant = yourHtmlInRows.Select(str =>
str.Replace("<br>", Environment.NewLine)
.Replace("<BR>", Environment.NewLine)).ToList();
File.WriteAllLines(ofd.FileName, resultYouWant);
}