Filter Html Result
I`m building a C# program to search and address and return the zip code,
the problem is when I search the zip code I gent only one answer and and
send it to a datagrid view with the code below but when I search an
address like here
http://www.buscacep.correios.com.br/servicos/dnec/consultaEnderecoAction.do?relaxation=itambe&TipoCep=ALL&semelhante=N&cfm=1&Metodo=listaLogradouro&TipoConsulta=relaxation&StartRow=1&EndRow=10
I get a table with many lines, I would like to send it to the datagridview
as a multiple lines table
:code Stream stream = resposta.GetResponseStream();
do
{
cont = stream.Read(buffer, 0, buffer.Length);
temp = Encoding.Default.GetString(buffer, 0, cont).Trim();
sb.Append(temp);
} while (cont > 0);
string pagina = sb.ToString();
if (pagina.IndexOf("<font color=\"black\">CEP NAO
INFORMADO</font>") >= 0)
{
MessageBox.Show("CEP NÃO INFORMADO!!", "Sem CEP");
}
//numero de ocorrencias da string
if (pagina.IndexOf("<font color=\"black\">CEP NAO
ENCONTRADO</font>") >= 0 | pagina.IndexOf("<font
color=\"black\">CEP NAO INFORMADO</font>") >= 0)
{
textBoxMostraTudo.Text = pagina;
textBoxMostraTudo.Text = "CEP não localizado.";
textBoxLogradouro.Text = "CEP não localizado.";
textBoxBairro.Text = "CEP não localizado.";
textBoxCidade.Text = "CEP não localizado.";
textBoxEstado.Text = "CEP não localizado.";
maskedTextBoxCep.Focus();
}
else
{
textBoxMostraTudo.Text = pagina;
string logradouro = Regex.Match(pagina,
"<td width=\"268\" style=\"padding:
2px\">(.*)</td>").Groups[1].Value;
string bairro = Regex.Matches(pagina,
"<td width=\"140\" style=\"padding:
2px\">(.*)</td>")[0].Groups[1].Value;
string cidade = Regex.Matches(pagina,
"<td width=\"140\" style=\"padding:
2px\">(.*)</td>")[1].Groups[1].Value;
string estado = Regex.Match(pagina,
"<td width=\"25\" style=\"padding:
2px\">(.*)</td>").Groups[1].Value;
string resultado = String.Format(
"Logradouro: {0} " + Environment.NewLine + "Bairro: {1}" +
Environment.NewLine + "Cidade: {2}" + Environment.NewLine +
"Estado: {3}",
logradouroLimpo, bairro, cidade, estado);
string cepCorreios = Regex.Match(pagina,
"<td width=\"65\" style=\"padding:
2px\">(.*)</td>").Groups[1].Value;
dataGridView1[0, 0].Value = logradouro;
//textBoxLogradouro.Text = logradouro;
dataGridView1[1, 0].Value = bairro;
textBoxBairro.Text = bairro;
dataGridView1[2, 0].Value = cidade;
textBoxCidade.Text = cidade;
dataGridView1[3, 0].Value = estado;
textBoxEstado.Text = estado;
dataGridView1[4, 0].Value = cepCorreios;
}
No comments:
Post a Comment