I have an HTML document with unclosed <LI> elements. I need to append </LI> to the end of each </OBJECT> that follows the opening <LI> tag.
Note: Objects that aren’t preceded by <LI> should not have an </LI> tag appended to </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>
This is what I’ve got so far with no luck
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();
}
UPDATE: I also tried this with no luck:
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());
}
This answer is just in terms of your update:
string.Replace returns a string. Strings are immutable in C# which means you can’t directly change the string. Any operation that appears to change the string actually returns one.
Therefore, this line:
..does nothing. It should be: