פורסם 2024 בנובמבר 201 שנה יש לי את הקוד הבא:מחלקת HtmlElement:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace p2{ public class HtmlElement { public string id { get; set; } public string name { get; set; } public List<string> attributes { get; set; } public List<string> classes { get; set; } public string innerHtml { get; set; } public HtmlElement parent { get; set; } public List<HtmlElement> children { get; set; } public HtmlElement() { attributes = new List<string>(); classes = new List<string>(); children = new List<HtmlElement>(); } public override string ToString() { return "["+"id: "+id+", name: "+name+", attributes: "+attributes.ToString()+", classes: "+classes.ToString() +", innerHtml: "+innerHtml+", parent: "+parent+", children: "+children.ToString()+" ]"; } }}מחלקת HtmlHelper:using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.Json;using System.Threading.Tasks;namespace p2{ public class HtmlHelper { private readonly static HtmlHelper instance = new HtmlHelper(); public static HtmlHelper Instance => instance; public string[] openCloseTags { get; set; } public string[] openTags { get; set; } public HtmlHelper() { openCloseTags= JsonSerializer.Deserialize<string[]>(File.ReadAllText("seed/HtmlTags.json")); openTags = JsonSerializer.Deserialize<string[]>(File.ReadAllText("seed/HtmlVoidTags.json")); } }}Program:using p2;using System.Text.RegularExpressions;var html = await Load("https://hebrewbooks.org/beis");var cleanHtml = new Regex(@"\s+").Replace(html, " ");var htmlLines = new Regex("<(.*?)>").Split(cleanHtml).Where(s => !string.IsNullOrWhiteSpace(s)).ToList();var htmlE = "<div id=\"my-id\" class=\"my-class-1 my-class-2\" width=\"100%\">text</div>";var attributes = new Regex("([^\\s]*?)=\"(.*?)\"").Matches(htmlE);//HtmlHelper a = new HtmlHelper();//Console.WriteLine();Console.ReadLine();var handler = new HttpClientHandler();async Task<string> Load(string url){ HttpClient client = new HttpClient(); var response = await client.GetAsync(url); var html = await response.Content.ReadAsStringAsync(); return html;}//בניית העץHtmlElement root = new HtmlElement();root.name = htmlLines[0];root.parent = null;HtmlElement tElement = new HtmlElement();string tempElement;string pattern = @"(\w+)=[""']?([^""' >]+)[""']?";List<string> allTags = new List<string>();foreach (var tag in HtmlHelper.Instance.openTags) allTags.Add(tag);foreach (var tag in HtmlHelper.Instance.openCloseTags) allTags.Add(tag);for (int i = 1; i < htmlLines.Count; i++){ var l = htmlLines[i].Split(" "); tempElement = l[0]; if (tempElement == "/html") break; if (tempElement[0] == '/') { tElement = tElement.parent; } else { if (tempElement == "html") { HtmlElement obj = new HtmlElement(); obj.name = tempElement; obj.parent = root; root.children.Add(obj); tElement = obj; } else { if (allTags.Contains(tempElement)) { HtmlElement obj = new HtmlElement(); obj.name = tempElement; obj.parent = tElement; var strLine = htmlLines[i].Substring(tempElement.Length); MatchCollection maches = Regex.Matches(strLine, pattern); foreach (Match m in maches) { obj.attributes.Add(m.Groups[1].Value); if (m.Groups[1].Value == "class") { foreach (var v in m.Groups[2].Value.Split(" ")) obj.classes.Add(v); } else if (m.Groups[1].Value == "id") obj.id = m.Groups[2].Value; } tElement.children.Add(obj); if (HtmlHelper.Instance.openCloseTags.Contains(tempElement)) { tElement = obj; } break; } else tElement.innerHtml = htmlLines[i]; } } HtmlElement ht = root;}Console.ReadLine();כל הזמן נופלת לי שגיאה שה-root שלי null, וגם כל אוביקט מסוג HtmlElement הוא nullכלומר יש איזה שגיאה בקוד.
הצטרפ/י לדיון
בשלב זה תוכל/י להצטרף לדיון, ולאחר מכן להצטרף לקהילה שלנו. אם כבר יש לך חשבון אצלנו, אנא התחבר/י עכשיו על מנת להגיב תחת שם המשתמש שלך.
לתשומת לבך: התגובה תופיע לגולשים לאחר אישור של צוות הנהלת הפורומים.