Boa noite,
Basicamente verifico cada Anexo e cada NCM. Criei um flag com 3 situações
0 => Não confirmado
1 => Confirmado
2 => Confirmado usando um método fraco
A rotina carece de mais testes e ajustes.
Veja arquivo em Anexo:
------------------------------------
tive que corrigir algumas despadronizações manualmente.
Exemplo:
--------------
var parse = new ParseAnexos();
parse.ParseFileName(@"d:\pagina.html");
Observe que cada ParseAnexo tem a informação de Anexo, lista de NCM e lista NBS. InnerTable permite um teste fraco através do método IndexOfTable.
Código:
------------
public class ParseAnexos : List<ParseAnexo>
{
public ParseAnexos()
{
}
public void ParseFileName(string fileName)
{
Parse(File.ReadAllText(fileName));
}
public void Parse(string text)
{
var startAnexoIndex = 0;
GetTag(text, "<p class=\"anexo\">", out string textAnexo, ref startAnexoIndex);
while (startAnexoIndex > 0 && textAnexo.StartsWith("ANEXO"))
{
var sbDescription = new StringBuilder();
var startTableIndex = startAnexoIndex;
GetTag(text, "<table class=\"dou-table\">", out string textTable, ref startTableIndex);
var startDescriptionIndex = startAnexoIndex;
GetTag(text, "<p class=\"dou-paragraph\">", out string textDescription, ref startDescriptionIndex);
while (startDescriptionIndex > 0 && startDescriptionIndex < startTableIndex)
{
textDescription = textDescription.Trim();
if (textDescription.Length > 0)
sbDescription.Append(textDescription).Append(' ');
GetTag(text, "<p class=\"dou-paragraph\">", out textDescription, ref startDescriptionIndex);
}
var anexo = new ParseAnexo(textAnexo.Substring(6), sbDescription.ToString().Trim(), textTable);
this.Add(anexo);
var isNBS = false;
var isNCM = false;
var startRowIndex = 0;
GetTag(textTable, "<tr>", out string textRow, ref startRowIndex);
while (startRowIndex > 0)
{
var startCellIndex = 0;
GetTag(textRow, "<td rowspan=\"1\" colspan=\"1\">", out string textCell, ref startCellIndex);
GetTag(textRow, "<td rowspan=\"1\" colspan=\"1\">", out textCell, ref startCellIndex);
GetTag(textRow, "<td rowspan=\"1\" colspan=\"1\">", out textCell, ref startCellIndex);
GetTag(textRow, "<p class=\"dou-paragraph\">", out textCell, ref startCellIndex);
while (startCellIndex >= 0)
{
if (textCell == "NBS") isNBS = true;
else if (textCell == "NCM/SH") isNCM = true;
else if (textCell == "NBS / NCM/SH" || textCell == "NBS/NCM")
{
//????
Console.WriteLine(textCell);
}
else if (string.IsNullOrEmpty(textCell))
{
//do nothing
}
else if (isNBS) anexo.NBS.Add(textCell);
else if (isNCM) anexo.NCM.Add(textCell);
GetTag(textRow, "<p class=\"dou-paragraph\">", out textCell, ref startCellIndex);
}
GetTag(textTable, "<tr>", out textRow, ref startRowIndex);
}
GetTag(text, "<p class=\"anexo\">", out textAnexo, ref startAnexoIndex);
}
}
public bool IndexOfTable(string anexo, string codiNCM)
{
var item = this.Find(x => x.Anexo == anexo);
return item != null && item.InnerTable.IndexOf(codiNCM) >= 0;
}
private static void GetTag(string text, string tag, out string innerText, ref int startIndex)
{
innerText = String.Empty;
if (startIndex >= 0)
{
var i = tag.IndexOf(' ');
var endTagName = i > 0 ? $"</{tag.Substring(1, i - 1)}>" : tag.Insert(1, "/");
var startTagIndex = text.IndexOf(tag, startIndex);
if (startTagIndex > 0)
{
startTagIndex = text.IndexOf('>', startTagIndex) + 1;
var endTagIndex = text.IndexOf(endTagName, startTagIndex);
innerText = startTagIndex > 0 && endTagIndex > 0 ? text.Substring(startTagIndex, endTagIndex - startTagIndex) : "";
startIndex = startTagIndex;
}
else
startIndex = -1;
}
}
}
public class ParseAnexo
{
public ParseAnexo(string anexo, string descricao, string innerTable)
{
Anexo = anexo;
Descricao = descricao;
InnerTable = innerTable;
NCM = new List<string>();
NBS = new List<string>();
}
public string Anexo { get; }
public string Descricao { get; }
public string InnerTable { get; } //Para tabelas não formatadas em 3 colunas e os códigos estão dentro de um texto
public List<string> NCM { get; }
public List<string> NBS { get; }
}