Beispiel #1
0
        private static object DomObjectsToNodeObjectsConvernet(HtmlNode node)
        {
            if (node.NodeType == HtmlNodeType.Text)
            {
                if (node.InnerText.Length == 0)
                {
                    return(null);
                }
                return(WebUtility.HtmlDecode(node.InnerText));
            }

            if (node.NodeType != HtmlNodeType.Element)
            {
                return(null);
            }
            var telegrahpNode = new TelegraphNode();

            telegrahpNode.tag = node.OriginalName.ToLower();

            var hrefValue = node.GetAttributeValue("href", null);
            var srcValue  = node.GetAttributeValue("src", null);

            if ((hrefValue != null || srcValue != null) && telegrahpNode.attrs == null)
            {
                telegrahpNode.attrs = new Dictionary <string, string>();
            }
            if (hrefValue != null)
            {
                telegrahpNode.attrs["href"] = hrefValue;
            }
            if (srcValue != null)
            {
                telegrahpNode.attrs["src"] = srcValue;
            }

            if (node.ChildNodes.Count > 0)
            {
                telegrahpNode.children = new List <object>();
                foreach (var childNode in node.ChildNodes)
                {
                    telegrahpNode.children.Add(DomObjectsToNodeObjectsConvernet(childNode));
                }
            }
            return(telegrahpNode);
        }
Beispiel #2
0
        private string TelegraphParserMethod(List <string> imageUrls, HtmlNode htmlNode)
        {
            var           listOfNodes = new List <Object>();
            TelegraphNode urlPNode    = new TelegraphNode()
            {
                tag = "p"
            };

            urlPNode.children = new List <object>();
            TelegraphNode urlNode = new TelegraphNode()
            {
                tag = "a"
            };

            urlNode.attrs         = new Dictionary <string, string>();
            urlNode.attrs["href"] = url;
            urlNode.children      = new List <object>();
            urlNode.children.Add("Джерело");
            urlPNode.children.Add(urlNode);
            listOfNodes.Add(urlPNode);
            foreach (var node in htmlNode.ChildNodes)
            {
                listOfNodes.Add(DomObjectsToNodeObjectsConvernet(node));
            }
            foreach (var item in imageUrls)
            {
                TelegraphNode imgNode = new TelegraphNode()
                {
                    tag = "img"
                };
                imgNode.attrs        = new Dictionary <string, string>();
                imgNode.attrs["src"] = item;
                listOfNodes.Add(imgNode);
            }
            string output = JsonConvert.SerializeObject(listOfNodes, Formatting.None, new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            return(output);
        }