C#删除重复的HTML行

时间:2015-07-13 17:53:32

标签: c# html

我使用C#生成HTML文件。使用对象及其属性,我将行添加到HTML表中。但是,即使列值正确,我也会看到一些我不想要的重复行。在做了一些谷歌搜索之后,我找到了一些人们为实现这个目的而发布的HTML功能,例如:

function removeDuplicateRows($table){
    function getVisibleRowText($row){
        return $row.find('td:visible').text().toLowerCase();
    }

    $table.find('tr').each(function(index, row){
        var $row = $(row);
        $row.nextAll('tr').each(function(index, next){
            var $next = $(next);
            if(getVisibleRowText($next) == getVisibleRowText($row))
                $next.remove();
        })
    });
}

var seen = {};
$('table tr').each(function() {
    var txt = $(this).text();
    if (seen[txt])
        $(this).remove();
    else
        seen[txt] = true;
});

但是,我不确定如何在我的c#代码中使用它们(可能来自我缺乏HTML编码)。当我尝试使用这些并用sw.WriteLine包装每一行时,我得到的只是函数的文本。所以我的问题是,如何在代码中删除重复的HTML表格行?

我有什么:

StreamWriter sw = new StreamWriter(Path.Combine(cdsPublishRoot, "GeneralActive.htm"));
            sw.WriteLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"  http://www.w3.org/TR/html4/strict.dtd  \">");
            sw.WriteLine("<html>");
            sw.WriteLine("<head>");

            sw.WriteLine("<meta http-equiv=\"Expires\" content=\"0\">");

            sw.WriteLine("<title>GPCSE-CDS General</title>");

            sw.WriteLine("</head>");
            sw.WriteLine("<body onmousemove=\"MouseMove(event)\">");
            sw.WriteLine("<script type=\"text/javascript\">");
            sw.WriteLine("DoMainMenu(false,\"SDS\");");
            sw.WriteLine("DoSdsSubMenu(true,\"ARCHIVE\");");
            sw.WriteLine("</script>");
            sw.WriteLine("<p class=Heading2><br>Active PCM General Requirement Specifications<br></p>");
            sw.WriteLine("<table cellpadding=5 cellspacing=0 border=1 width=760 id=\"MainDataTable\">");
            sw.WriteLine("<tr><th style=\"width:14%\" align=left class=SdsHeader>Spec.</th>");
            sw.WriteLine("<th style=\"width:64%\" align=left class=SdsHeader>Specification Name</th>");
            sw.WriteLine("<th style=\"width:7%\" align=left class=SdsHeader>Spec. Ver.</th>");
            sw.WriteLine("<th style=\"width:7%\" align=left class=SdsHeader>Rev. Level</th>");
            sw.WriteLine("<th style=\"width:8%\" align=left class=SdsHeader>Rev. Date</th></tr>");


            foreach (string file in genActive.Distinct())
            {               
                string[] genOldDirectories = file.Split(Path.DirectorySeparatorChar);
                string path = Path.GetDirectoryName(file);
                string[] files = Directory.GetFiles(path);
                var acro = ArdApp.Data.GeneralSpecification.GeneralSpecVersion.DataLoad(genOldDirectories[2].ToString(), Convert.ToInt32(genOldDirectories[3][0].ToString()), Convert.ToInt32(genOldDirectories[3][2].ToString() + genOldDirectories[3][3].ToString()));
                    sw.WriteLine("<tr><td id=\"idTDMenuTitle0008\">");
                    sw.WriteLine("<div id=\"idMenuTitle0008\" class=\"clsMenuTitle\" onclick=\"DoMenu(this)\">");
                    sw.WriteLine(genOldDirectories[2] + "</div>");
                    sw.WriteLine("<div id=\"idMenu0008\" class=\"clsMenu\">");
                    sw.WriteLine("<ul class=\"clsMenuBullets\">");
                    foreach (var fileName in files)
                    {
                        string[] fileDirectory = fileName.Split(Path.DirectorySeparatorChar);
                        sw.WriteLine("<li class=\"clsMenuItem\"><a class=\"clsMenuItem\" href=" + StripToWebFriendlyName(file) + ">" + StripToWebFriendlyName(fileDirectory[5]) + "</a></li>");
                    }
                    sw.WriteLine("</ul>");
                    sw.WriteLine("</div></td>");
                    sw.WriteLine("<td class=BodyText>" + acro.AcronymName + "</td>");
                    sw.WriteLine("<td class=BodyText>" + Convert.ToInt32(genOldDirectories[3][0].ToString()) + "</td>");
                    sw.WriteLine("<td class=BodyText>" + genOldDirectories[3][2].ToString() + genOldDirectories[3][3] + "</td>");
                    sw.WriteLine("<td class=BodyText>" + acro.RevisionDate + "</td>");
                    sw.WriteLine("</tr>");


            }

            sw.WriteLine("</table>");
            sw.WriteLine("</Body>");
            sw.WriteLine("</HTML>");

            sw.Close();
        }

我尝试了什么:

...

            foreach (var fileName in files)
            {
                string[] fileDirectory = fileName.Split(Path.DirectorySeparatorChar);
                sw.WriteLine("<li class=\"clsMenuItem\"><a class=\"clsMenuItem\" href=" + StripToWebFriendlyName(file) + ">" + StripToWebFriendlyName(fileDirectory[5]) + "</a></li>");
            }
            sw.WriteLine("</ul>");
            sw.WriteLine("</div></td>");
            sw.WriteLine("<td class=BodyText>" + acro.AcronymName + "</td>");
            sw.WriteLine("<td class=BodyText>" + Convert.ToInt32(genOldDirectories[3][0].ToString()) + "</td>");
            sw.WriteLine("<td class=BodyText>" + genOldDirectories[3][2].ToString() + genOldDirectories[3][3] + "</td>");
            sw.WriteLine("<td class=BodyText>" + acro.RevisionDate + "</td>");
            sw.WriteLine("</tr>");


    }

        sw.WriteLine("var seen = {};");
        sw.WriteLine("$('table tr').each(function() {");
        sw.WriteLine("var txt = $(this).text();");
        sw.WriteLine("if (seen[txt])");
        sw.WriteLine(" $(this).remove();");
        sw.WriteLine("else");
        sw.WriteLine("seen[txt] = true;");
        sw.WriteLine("});");

1 个答案:

答案 0 :(得分:2)

看起来您只是阅读文件信息,只想在HTML显示中显示不同的文件?

如果这仍然是ac#question,请考虑将文件信息(完整文件路径信息)读入List<string>,然后您可以使用linq“Distinct”获取非重复项目,然后输出在html中根据需要。