C#读取文件并显示在表中

C#读取文件并显示在表中,c#,C#,我的目标是读取文本文件并在表中显示内容。 我已经通过了很多论坛和目瞪口呆,仍然找不到解决办法 我的表格格式: <table > <`tr> <`td> Australia <`/td> <`td> <`table> <`tr> <`td>

我的目标是读取文本文件并在表中显示内容。 我已经通过了很多论坛和目瞪口呆,仍然找不到解决办法

我的表格格式:

<table >

   <`tr>

      <`td>

         Australia

      <`/td>

      <`td>

         <`table>

            <`tr>

               <`td>

                  1

               <`/td>

            <`/tr>

         <`/table>

       <`/tr>

<`/table>
我的阅读代码:

        string path = @"..\TestFile.txt";
        char token = ',';
        char token2 = '=';

        string[] lines = File.ReadAllLines(path);

        Response.Write("<table>");
        foreach (string line in lines)
        {
            string[] country = line.Split(token2);
            string[] image = line.Split(token);
            string row = "<tr><td>" + country + "</td>" +"<table><tr>";
            Response.Write(row);
            for (int i = 0; i < image.Length; i++)
            {   

                string row2 = "<td>" + image[i] +"</td>";
                Response.Write(row2);
            }
            Response.Write("</tr></table>");
        }
        Response.Write("</tr></table>");
我想要实现的是:

Australia    1     1    2    2

Malaysia     1     1    1    2    2    2

Singapore    1     1    1    1    2    2    2    2

任何帮助都将不胜感激。谢谢

如果我正确理解了这个问题,试着这样做:

string[] country = line.Split(token2);
string[] image = country[1].Split(token); //<- take string after = symbol, and split it
string row = "<tr><td>" + country[0] + "</td>" +"<table><tr>"; //<- take first string before = symbol
string[]country=line.Split(token2);

字符串[]图像=国家[1]。拆分(令牌)// 通过快速阅读问题,您似乎已经能够拆分/解析内容,并且只需要一种格式化输出的方法

这可以通过使用
String.padlight()
String.PadRight()
实现。或者
String.Format(“{0,-10}”,stringValue)

嗯,

参考资料


@失望先生,我想我们应该为他编译并完成它;)很抱歉给您添麻烦,我已经尝试了很长时间了=[分割不正确,因为澳大利亚没有像表中那样用1分割,我需要它看起来像澳大利亚1 1 1 1 1 foo:提示:调试并检查
country
的类型和
image[0]的值]
。感谢您的回复。问题已解决=]谢谢!这解决了问题=]但如何在二维数组中实现它?这是另一个问题。:)但我认为你们应该把所有的东西都储存在清单或字典里。
Australia    1     1    2    2

Malaysia     1     1    1    2    2    2

Singapore    1     1    1    1    2    2    2    2
string[] country = line.Split(token2);
string[] image = country[1].Split(token); //<- take string after = symbol, and split it
string row = "<tr><td>" + country[0] + "</td>" +"<table><tr>"; //<- take first string before = symbol