从网站上逐行正确解析字符串

时间:2013-03-12 02:10:48

标签: c# listview split

我将此回显到一个空白页面:

echo "Testing|Testing1|Testing2|Testing3|Testing4<br/>";
echo "Something|Something1|Something2|Something3|Something4";

现在我有了一个列表视图。在此示例中,它将创建包含5列的2行。所以我的问题是,如何逐行阅读以正确创建网站上显示的行数?

到目前为止,这是我的代码:

WebClient client = new WebClient();
string downloadString = client.DownloadString("https://example.com/Testing.php");

string[] downloadString2 = downloadString.Split(
    new char[] 
    { 
        (char)'|' 
    }, System.StringSplitOptions.RemoveEmptyEntries);

ListViewItem item = new ListViewItem(
    new[] 
    { 
        downloadString2[0].ToString(), 
        downloadString2[1].ToString(), 
        downloadString2[2].ToString(), 
        downloadString2[3].ToString(), 
        downloadString2[4].ToString() 
    });
listView1.Items.Add(item);

(列已在列表视图中创建)

- 编辑:这对我来说很好:

        WebClient client = new WebClient();
        string downloadString = client.DownloadString("https://example.com/Testing.php");

        string[] stringSeparators = new string[] { "<br/>" };
        string[] Lines = downloadString.Split(stringSeparators, StringSplitOptions.None);

        string[] things = new string[5]; // Fixed size. I might find a way later to make it dynamically
        int i = 0;

        foreach (string line in Lines)
        {
            string[] words = line.Split('|');
            i = 0;
            foreach (string word in words)
            {
                things[i] = word;
                i++;

            }

            ListViewItem item = new ListViewItem(
                new[] 
                { 
                    things[0], 
                    things[1], 
                    things[2], 
                    things[3], 
                    things[4] 
                });
            listView1.Items.Add(item);
        }

1 个答案:

答案 0 :(得分:3)

不完全是你想要的,但你可以试试这个

   WebClient client = new WebClient();
   string downloadString = client.DownloadString("https://example.com/Testing.php");

   string[] stringSeparators = new string[] {"<br/>"};
   string[] Lines = downloadString.Split(stringSeparators, StringSplitOptions.None);
   foreach (string line in Lines)
   {
        string[] words = line.Split('|');
        foreach (string word in words)
        {

                 ListViewItem item = new ListViewItem();
                 item.add(word);

        }
       listView1.Items.Add(item);
    }