简体   繁体   English

最有效的解析分隔成html表的方法c#

[英]Most efficient way to parse delimited into html table c#

I've got the following delimited string with pairs: 我有以下带分隔的分隔字符串:

1,5|2,5|3,5

I want to create a table as follows: 我想创建一个表如下:

< table>
     < tr>< td>1< /td>< td>5< /td>< /tr>
     < tr>< td>2< /td>< td>5< /td>< /tr>
     < tr>< td>3< /td>< td>5< /td>< /tr>
     < /table>

What's the most efficient way in C#? 什么是C#中最有效的方式?

Parse the string (simple splitting should be enough) and I'd suggest using the .NET XML classes (or Html Agility Pack for the purists out there) to generate the table. 解析字符串(简单的拆分应该足够了)我建议使用.NET XML类(或者那里的纯粹主义者的Html Agility Pack )来生成表。 Might be overkill vs building up the string manually especially for simple data but it is less verbose and should be easier to extend later. 可能是过度杀戮而不是手动构建字符串,尤其是对于简单的数据,但它不那么冗长,以后应该更容易扩展。

Using LINQ to XML: 使用LINQ to XML:

var str = "1,5|2,5|3,5";
var table =
    new XElement("table",
        str.Split('|')
           .Select(pair =>
               new XElement("tr",
                   pair.Split(',')
                       .Select(num => new XElement("td", num))
               )
           )
    ).ToString();

Yields the string: 产生字符串:

<table>
  <tr>
    <td>1</td>
    <td>5</td>
  </tr>
  <tr>
    <td>2</td>
    <td>5</td>
  </tr>
  <tr>
    <td>3</td>
    <td>5</td>
  </tr>
</table>

Version 1: Straight-forward 版本1:直接前进

String html = "<table>";
Array.ForEach<String>("1,5|2,5|3,5".Split('|'),r =>
{
  html += "<tr>";
  Array.ForEach(r.Split(','),c =>
  {
    html += String.Format("<td>{0}</td>", c);
  });
  html += "</tr>";
});
html += "</table>";

Untested, but something of the sort? 未经测试,但有类似的东西? I take it back, battle tested and working. 我把它拿回来, 经过测试和工作。

Version two, less the delegate: 版本2,减去代表:

String html = "<table>";
foreach (String r in "1,5|2,5|3,5".Split('|'))
{
  html += "<tr>";
  foreach (String c in r.Split(','))
    html += String.Format("<td>{0}</td>", c);
  html += "</tr>";
}
html += "</table>";

Both versions in a working demo. 两个版本都在一个工作演示中。

And Another version which includes StringBuilder 另一个版本包括StringBuilder

If you search for efficient way, then you shouldn't use string concat, use StringBuilder instead: 如果你搜索有效的方法,那么你不应该使用字符串concat,而是使用StringBuilder

    private static string ToTable(string input)
    {
        var result = new StringBuilder(input.Length * 2);
        result.AppendLine("<table>");
        foreach (var row in input.Split('|'))
        {
            result.Append("<tr>");
            foreach (var cell in row.Split(','))
                result.AppendFormat("<td>{0}</td>", cell);
            result.AppendLine("/<tr>");
        }
        result.AppendLine("</table>");
        return result.ToString();
    }

Create a IList from your collection as described above using the String.Split method in the code behind and use the native DataList UI Control , bind the datasource to the control and set the DataSource property of the control to your List. 如上所述使用后面的代码中的String.Split方法从集合中创建IList,并使用本机DataList UI Control ,将数据源绑定到控件并将控件的DataSource属性设置为List。

 <asp:DataList ID="YourDataList" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Horizontal" runat="server">
     <ItemTemplate>
        <%# Eval("value") %> 
     </ItemTemplate>
 </asp:DataList>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM