正确解析字符串一行一行从网站
本文关键字:一行 网站 字符串 | 更新日期: 2023-09-27 18:03:20
我把这个回调到空白页:
echo "Testing|Testing1|Testing2|Testing3|Testing4<br/>";
echo "Something|Something1|Something2|Something3|Something4";
现在我有一个listview。在本例中,它将创建2行5列。所以我的问题是,如何逐行阅读,以正确创建在网站上显示的行数?
下面是目前为止的代码: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);
(列已经在listview中创建)
——编辑:这对我来说很好:
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);
}
不完全是你想要的,但你可以试试这个
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);
}