【问题标题】:Changing sepecific text in a specific line in text file更改文本文件中特定行中的特定文本
【发布时间】:2014-08-18 05:57:16
【问题描述】:

这是我要更改的 aspx 文件中的文本行

<center><body link=blue vlink=purple class=xl65 onload="processData();"><form id="mainform"
 action="http://localhost/XLEZ/DataHandler/Submit.aspx" method="post" enctype="multipart/form-
data"><input type="hidden" id="hid_extra" name="hid_extra" 
value="Machine_Inspection_20140807162226.xlsx||Machine_Inspection||Excavator 
Inspection||Excavator Inspection|Forklift Inspection|Tractor Inspection"/>

我的代码找到了这一行,我想在这一行改变form的动作,

这是我的代码,它基本上改变了整行,但我只想更改特定的文本

String Form_action ="http://\" + Request.Url.Authority+\"/XLEZ/DataHandler/Submit.aspx\"";

while ((line = sr.ReadLine()) != null)
                        {

                            if (line.Contains("form id=\"mainform\""))
                            {
                                index = count;
                            }
                            count++;
                        }
                        sr.Dispose();
                    }
                    if (index != 0)
                    {
                        var lines = File.ReadAllLines(selected_path);
                        lines[index] = Form_action ;
                        File.WriteAllLines(selected_path, lines);
                    }

但这用action替换了整行,我只是想改变这一行中的action

【问题讨论】:

  • 你为什么不用HTML Agility Pack
  • @HassanNisar 使用 Visual Studio 2008 和 DOT NET Framework 2.0
  • 是的 .Net 2.0 也可以使用 dll。

标签: c# visual-studio c#-2.0


【解决方案1】:

在您的代码中,这行代码显然替换了整行 HTML 代码:

lines[index] = Form_action ;

您需要替换此行中的部分字符串。例如,您可以执行以下操作:

String Form_action ="http://\" + Request.Url.Authority+\"/XLEZ/DataHandler/Submit.aspx\"";

while ((line = sr.ReadLine()) != null)
                        {
                        if (line.Contains("form id=\"mainform\""))
                        {
                            index = count;
                        }
                        count++;
                    }
                    sr.Dispose();
                }
                if (index != 0)
                {
                    var lines = File.ReadAllLines(selected_path);
                    int start = lines[index].IndexOf("action");
                    string newLine = lines[index].Substring(0, start + 8) + Form_action + " " + lines[index].Substring(lines[index].IndexOf("method"));
                    lines[index] = newLine;
                    File.WriteAllLines(selected_path, lines);
                }

您的“Form_Action”变量不会保存正确的值,因为您在使用 Request 对象之前转义了“。您应该看看这个。

调整了表单动作的创建:

String Form_action ="http://" + Request.Url.Authority + "/XLEZ/DataHandler/Submit.aspx\"";

【讨论】:

  • 我怎样才能在这一行中找到 indexOf "/XLEZ",就像你在回答中使用 "action" 和 "method" 所做的那样
  • 我只想在 htp//: 之后和 /XLEZ 之前放置 "++" 以获取当前 IP 地址...
  • 您可以像其他字符串一样使用 'lines[index].IndexOf("XLEZ")' 找到它,或者您只需调整 Form_action 字符串的创建。我会用调整后的版本更新我的答案。
【解决方案2】:

您可以使用正则表达式以更简单的方式做到这一点:

    Regex regex = new Regex(".*form id=\"mainform\".* action=\"(.+?)\" .*");

    var lines = File.ReadAllLines(selected_path);
    foreach (string line in lines)
    {
        Match match = regex.Match(line);
        if (match.Success)
        {
            string toReplace = match.Groups[1].Value;
            lines[count] = lines[count].Replace(toReplace, Form_action);
        }
        count++;
    }
    File.WriteAllLines(selected_path, lines);

【讨论】:

    猜你喜欢
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-02
    • 1970-01-01
    • 2023-03-23
    • 2020-08-08
    • 1970-01-01
    相关资源
    最近更新 更多