【发布时间】:2019-08-14 19:08:46
【问题描述】:
我有一个无序列表作为字符串存储在我的 SQL 表中。我需要稍后在 Excel 文件中显示它,但我无法正确格式化它。
我尝试过使用正则表达式,我想我已经接近了,但我错过了一些东西。
这是我的示例输入字符串
<ul>
<li>Line 1</li>
<li>Line 2</li>
<li>Line 3</li>
<li>Line 4
<ul style="list-style-type:circle">
<li>Line 4-1</li>
<li>Line 4-2
<ul style="list-style-type:square">
<li>Line 4-2-1</li>
<li>Line 4-2-2</li>
<li>Line 4-2-3</li>
</ul>
</li>
<li>Line 4-3</li>
</ul>
</li>
<li>Line 5</li>
<li>Line 6</li>
<li>Line 7</li>
</ul>
这是我到目前为止所做的。
var dt = new DataTable();
dt.Columns.Add();
string inputValue; //unordered list from above
Regex rgxLI = new Regex(@"<li>(.*?)</li>");
Regex rgxCircle = new Regex(@"<ul style=\""list-style-type:circle\"">(.*?)</ul>");
Regex rgxSquare = new Regex(@"<ul style=\""list-style-type:square\"">(.*?)</ul>");
MatchCollection mcLI = rgxLI.Matches(inputValue);
for (var i = 0; i < mcLI.Count; i++)
{
DataRow dr = dt.NewRow();
//string instructionLine = mc[i].Value;
if (mcLI[i].Value.Contains("<ul style=\"list-style-type:circle\">"))
{
MatchCollection mcCircle = rgxCircle.Matches(mcLI[i].Value);
for (var j = 0; j < mcCircle.Count; j++)
{
if (mcLI[j].Value.Contains("<ul style=\"list-style-type:square\">"))
{
MatchCollection mcSquare= rgxSquare.Matches(mcLI[j].Value);
dr[0] = System.Net.WebUtility.HtmlDecode("▪" + mcSquare[j].ToString().Replace("<li>", "").Replace("</li>", ""));
}
else
{
dr[0] = System.Net.WebUtility.HtmlDecode("•" + mcCircle[j].ToString().Replace("<li>", "").Replace("</li>", ""));
}
}
}
else
{
dr[0] = System.Net.WebUtility.HtmlDecode(mcLI[i].Value.Replace("<li>", "").Replace("</li>", ""));
}
dt.Rows.Add(dr);
}
不确定我是否过于复杂或只是错过了一些步骤。我能够解析大部分字符串,但在 4-1 之后我错过了要点。
【问题讨论】:
标签: c# asp.net-mvc html-lists generic-list