GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — DocumentGenerator/MergeHtml.linq at master · ewdlop/DocumentGenerator Expand file tree/ Copy pathMergeHtml.linqMore file actions 163 lines (135 loc) · 3.45 KB / Copy pathMergeHtml.linqFile metadata and controls163 lines (135 loc) · 3.45 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 <Query Kind="Statements"> <NuGetReference>HtmlAgilityPack</NuGetReference> <NuGetReference>Newtonsoft.Json</NuGetReference> <Namespace>HtmlAgilityPack</Namespace> </Query> using HtmlAgilityPack; var htmlDoc1 = """ <html> <body> <div class='container'> <p id='paragraph1'>Hello</p> </div> </body> </html> """; var htmlDoc2 = """ <html> <body> <div class='container'> <p id='paragraph2'>World</p> </div> <footer>Footer content</footer> </body> </html> """; var htmlDoc3 = """ <html><body><div>456<div><div class="123"></div></body><footer>1</footer></html> """; var htmlDoc4 = """ <html><body><div>456<div><div class="123"></div><div>test</div></body><footer id="123"></footer></html> """; //HtmlDocument doc = new HtmlDocument(); //doc.LoadHtml(htmlDoc1); // //HtmlDocument doc2 = new HtmlDocument(); //doc2.LoadHtml(htmlDoc2); // //var list = doc.DocumentNode //.Descendants().ToList(); //list.AddRange(doc2.DocumentNode // .Descendants()); //list = list.DistinctBy(n => n.InnerHtml).ToList(); //HtmlDocument doc3 = new HtmlDocument(); //list.ForEach(n => doc3.DocumentNode.AppendChild(n)); //GetLeafNodes(doc2.DocumentNode).Dump(); MergeHtml(htmlDoc1, htmlDoc2); static void MergeHtml(string html1, string html2) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html1); var htmlParseErrors = doc.ParseErrors; if (htmlParseErrors is not null && htmlParseErrors.Any()) { return; } doc.Dump(); HtmlDocument doc2 = new HtmlDocument(); doc2.LoadHtml(html2); var htmlParseErrors2 = doc.ParseErrors; if (htmlParseErrors2 is not null && htmlParseErrors2.Any()) { return; } MergeHtmlDocuments(doc, doc2).DocumentNode.OuterHtml.Dump(); } static HtmlDocument MergeHtmlDocuments(HtmlDocument doc1, HtmlDocument doc2) { var mergedDoc = new HtmlDocument(); mergedDoc.LoadHtml(doc1.DocumentNode.OuterHtml); MergeNodes(mergedDoc.DocumentNode, doc2.DocumentNode); return mergedDoc; } static void MergeNodes(HtmlNode node1, HtmlNode node2) { foreach (var child2 in node2.ChildNodes) { var matchingChild1 = FindMatchingNode(node1, child2); if (matchingChild1 != null) { MergeNodes(matchingChild1, child2); } else { var importedChild = HtmlNode.CreateNode(child2.OuterHtml); node1.AppendChild(importedChild); } } } static HtmlNode FindMatchingNode(HtmlNode parent, HtmlNode target) { return parent.ChildNodes .FirstOrDefault(child => NodesAreSimilar(child, target)); } static bool NodesAreSimilar(HtmlNode node1, HtmlNode node2) { return node1.Name == node2.Name && node1.Attributes["class"]?.Value == node2.Attributes["class"]?.Value && node1.Attributes["id"]?.Value == node2.Attributes["id"]?.Value; } static HtmlNode MergeNodes2(HtmlNode? node1, HtmlNode? node2) { if (node1 is null) return node2; if (node2 is null) return node1; return node1; } static List<HtmlNode> GetLeafNodes(HtmlNode node) { var leafNodes = new List<HtmlNode>(); foreach (var child in node.ChildNodes) { if (child.NodeType == HtmlNodeType.Element) { if (IsLeafNode(child)) { leafNodes.Add(child); } else { leafNodes.AddRange(GetLeafNodes(child)); } } } return leafNodes; } static bool IsLeafNode(HtmlNode node) { foreach (var child in node.ChildNodes) { if (child.NodeType == HtmlNodeType.Element) { return false; } } return true; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |