Yahoo Clever wird am 4. Mai 2021 (Eastern Time, Zeitzone US-Ostküste) eingestellt. Ab dem 20. April 2021 (Eastern Time) ist die Website von Yahoo Clever nur noch im reinen Lesemodus verfügbar. Andere Yahoo Produkte oder Dienste oder Ihr Yahoo Account sind von diesen Änderungen nicht betroffen. Auf dieser Hilfeseite finden Sie weitere Informationen zur Einstellung von Yahoo Clever und dazu, wie Sie Ihre Daten herunterladen.
Urs
C#: Filling Treeview recursively not working as expected?
I want to populate a TreeView control using C#. It should display the hierarchy of all links ("<A"-tags) in a Html-Page.
The page is crawled recursively, a new recusive call occurs for every direct child html-tag. A new node should be created and added under its respective parent for every a-tag encountered.
The caller clear() the nodes, creates/adds a root node, and starts the recursion (calls AddNode()) the newly created node and the Html-body with all its content.
My problem:
If i blindly create a (sub)node for each html-tag the treeview is built and structured as expected. But if I create a (sub)node for A-Tags (HTML links) only (uncomment all code to do so) i get a flat list instead, no hierarchy at all.
Any suggestions?
Code:
void Caller(){
this.trvUrls.Nodes.Clear();
TreeNode root = this.trvUrls.Nodes.Add(wbIE.Document.Url.AbsoluteUri);
HtmlElementCollection htmlBody = wbIE.Document.GetElementsByTagName("Body");
AddNode(root, htmlBody[0]);
}
private void AddNode(TreeNode oldParent, HtmlElement elt)
{
Regex regex = new Regex("href=..([^\"]*?[\"])");
TreeNode newParent = oldParent;
if (elt.TagName.ToLower() == "a")
{
string tag = regex.Match(elt.OuterHtml).Value.Replace("href=\"", "").TrimEnd('"');
newParent = oldParent.Nodes.Add(tag);
}
foreach (HtmlElement child in elt.Children)
{
AddNode(newParent, child);
}
}
1 AntwortProgramming & Designvor 7 Jahren