我有一个带有未关闭的<LI>
元素的HTML文档。我需要将</LI>
附加到开头</OBJECT>
标记后面的每个<LI>
的末尾。
注意:不在<LI>
之前的对象不应将</LI>
标记附加到</OBJECT>
<OBJECT value="example">
<param name="Joe">
</OBJECT>
<UL>
<LI> <OBJECT type="example">
<param name="Pat">
<param name="State" value="Arizona">
</OBJECT>
<UL>
<LI> <OBJECT type="example">
<param name="Steve">
<param name="State" value="California">
</OBJECT>
<OBJECT type="text/sitemap">
<param name="Carol">
</OBJECT>
这是我到目前为止没有运气的原因
private void closeListItems(string doc)
{
StringBuilder sb = new StringBuilder();
Regex rx = new Regex("(<LI>.(.+?)</OBJECT>)", RegexOptions.Multiline | RegexOptions.IgnoreCase);
string[] hhcFile = File.ReadAllLines(doc);
string temp = "";
foreach (string line in hhcFile)
{
temp += line + "\n";
}
temp = rx.Replace(temp, "<LI>");
StreamWriter sw = new StreamWriter(Application.StartupPath + "\\liFix.txt");
sw.Write(temp);
sw.Close();
}
更新:我也试过这个没有运气:
private void closeListItems(string doc)
{
StringBuilder sb = new StringBuilder();
string[] hhcFile = File.ReadAllLines(doc);
string temp = "";
bool liOpen = false;
foreach (string line in hhcFile)
{
temp = line;
if (line.Contains("<LI>"))
{
liOpen = true;
}
if (line.Contains("</OBJECT>") && liOpen == true)
{
temp.Replace(temp, temp + "</LI>");
liOpen = false;
}
sb.Append("\n" + temp);
}
File.WriteAllText("fixLi.txt", sb.ToString());
}
答案 0 :(得分:2)
这个答案仅就您的更新而言:
string.Replace返回一个字符串。字符串在C#中是不可变的,这意味着您无法直接更改字符串。任何看似改变字符串的操作实际上都返回一个。
因此,这一行:
temp.Replace(temp, temp + "</LI>");
..什么也没做。它应该是:
temp = temp.Replace(temp, temp + "</LI>");