Ejemplo n.º 1
0
        public IList<PostResultItem> GetDataItems(HtmlDocument doc, string div)
        {
            var dataItems = new List<PostResultItem>();
            var tablesWorked = new List<HtmlNode>();
            var nfeDiv = doc.GetElementbyId(div);
            Check(nfeDiv);

            var count = 1;
            foreach (var fieldset in nfeDiv.Descendants().Where(d => d.Name.ToLower() == "fieldset").ToList())
            {
                var legend = fieldset.Descendants().FirstOrDefault(d => d.Name.ToLower() == "legend");
                var tables = fieldset.Descendants().Where(d => d.Name.ToLower() == "table").ToList();
                var eEventosNfe = false;
                if (legend != null)
                    eEventosNfe = legend.InnerText.Trim().Contains("Situação Atual:");

                foreach (var table in tables)
                {
                    if (!tablesWorked.Contains(table))
                    {
                        if (table != null)
                        {
                            var trs = table.Descendants().Where(e => e.Name.ToLower() == "tr").ToList();

                            if (!eEventosNfe)
                            {
                                foreach (var tr in trs)
                                {
                                    var tds = tr.Descendants().Where(e => e.Name.ToLower() == "td").ToList();
                                    foreach (var td in tds)
                                    {
                                        if (legend != null)
                                        {
                                            var dataItem = new PostResultItem
                                            {
                                                Fieldset = count,
                                                Legend = legend.InnerText.Trim(),
                                                Div = div
                                            };

                                            var label = td.Descendants().FirstOrDefault(d => d.Name.ToLower() == "label");
                                            var span = td.Descendants().FirstOrDefault(d => d.Name.ToLower() == "span");

                                            if (label != null && span != null && !string.IsNullOrWhiteSpace(label.InnerText))
                                            {
                                                dataItem.AttributeName = label.InnerText.Trim();
                                                dataItem.AttributeValue = span.InnerText.Trim();
                                                dataItems.Add(dataItem);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 1; i < trs.Count; i++)
                                {
                                    var subNodeScience = trs[i].Descendants().FirstOrDefault(e => e.Id.Contains("CienciaOperacao"));

                                    if (subNodeScience != null)
                                        subNodeScience.Remove();

                                    var tds = trs[i].Descendants().Where(e => e.Name.ToLower() == "td").ToList();
                                    var position = 0;
                                    foreach (var td in tds)
                                    {
                                        var dataItem = new PostResultItem();
                                        switch (position)
                                        {
                                            case 0:
                                                dataItem.AttributeName = "Eventos da NF-e";
                                                break;
                                            case 1:
                                                dataItem.AttributeName = "Protocolo";
                                                break;
                                            case 2:
                                                dataItem.AttributeName = "Data / Hora";
                                                break;
                                            case 3:
                                                dataItem.AttributeName = "Data / Hora AN";
                                                break;
                                        }

                                        var span = td.Descendants().FirstOrDefault(d => d.Name.ToLower() == "span");
                                        if (span != null)
                                            dataItem.AttributeValue = span.InnerText.Trim();
                                        dataItem.Fieldset = count;
                                        dataItem.Legend = legend.InnerText.Trim();
                                        dataItem.Div = div;
                                        dataItems.Add(dataItem);
                                        position++;
                                    }
                                }
                                eEventosNfe = false;
                            }
                            tablesWorked.Add(table);
                        }
                    }
                }
                count++;
            }

            return dataItems.OrderBy(d => d.Fieldset).Distinct().ToList();
        }
Ejemplo n.º 2
0
        public IList <PostResultItem> GetDataItems(HtmlDocument doc, string div)
        {
            var dataItems    = new List <PostResultItem>();
            var tablesWorked = new List <HtmlNode>();
            var nfeDiv       = doc.GetElementbyId(div);

            Check(nfeDiv);

            var count = 1;

            foreach (var fieldset in nfeDiv.Descendants().Where(d => d.Name.ToLower() == "fieldset").ToList())
            {
                var legend      = fieldset.Descendants().FirstOrDefault(d => d.Name.ToLower() == "legend");
                var tables      = fieldset.Descendants().Where(d => d.Name.ToLower() == "table").ToList();
                var eEventosNfe = false;
                if (legend != null)
                {
                    eEventosNfe = legend.InnerText.Trim().Contains("Situação Atual:");
                }

                foreach (var table in tables)
                {
                    if (!tablesWorked.Contains(table))
                    {
                        if (table != null)
                        {
                            var trs = table.Descendants().Where(e => e.Name.ToLower() == "tr").ToList();

                            if (!eEventosNfe)
                            {
                                foreach (var tr in trs)
                                {
                                    var tds = tr.Descendants().Where(e => e.Name.ToLower() == "td").ToList();
                                    foreach (var td in tds)
                                    {
                                        if (legend != null)
                                        {
                                            var dataItem = new PostResultItem
                                            {
                                                Fieldset = count,
                                                Legend   = legend.InnerText.Trim(),
                                                Div      = div
                                            };

                                            var label = td.Descendants().FirstOrDefault(d => d.Name.ToLower() == "label");
                                            var span  = td.Descendants().FirstOrDefault(d => d.Name.ToLower() == "span");

                                            if (label != null && span != null && !string.IsNullOrWhiteSpace(label.InnerText))
                                            {
                                                dataItem.AttributeName  = label.InnerText.Trim();
                                                dataItem.AttributeValue = span.InnerText.Trim();
                                                dataItems.Add(dataItem);
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 1; i < trs.Count; i++)
                                {
                                    var subNodeScience = trs[i].Descendants().FirstOrDefault(e => e.Id.Contains("CienciaOperacao"));

                                    if (subNodeScience != null)
                                    {
                                        subNodeScience.Remove();
                                    }

                                    var tds      = trs[i].Descendants().Where(e => e.Name.ToLower() == "td").ToList();
                                    var position = 0;
                                    foreach (var td in tds)
                                    {
                                        var dataItem = new PostResultItem();
                                        switch (position)
                                        {
                                        case 0:
                                            dataItem.AttributeName = "Eventos da NF-e";
                                            break;

                                        case 1:
                                            dataItem.AttributeName = "Protocolo";
                                            break;

                                        case 2:
                                            dataItem.AttributeName = "Data / Hora";
                                            break;

                                        case 3:
                                            dataItem.AttributeName = "Data / Hora AN";
                                            break;
                                        }

                                        var span = td.Descendants().FirstOrDefault(d => d.Name.ToLower() == "span");
                                        if (span != null)
                                        {
                                            dataItem.AttributeValue = span.InnerText.Trim();
                                        }
                                        dataItem.Fieldset = count;
                                        dataItem.Legend   = legend.InnerText.Trim();
                                        dataItem.Div      = div;
                                        dataItems.Add(dataItem);
                                        position++;
                                    }
                                }
                                eEventosNfe = false;
                            }
                            tablesWorked.Add(table);
                        }
                    }
                }
                count++;
            }

            return(dataItems.OrderBy(d => d.Fieldset).Distinct().ToList());
        }