编辑:我意识到我似乎马上来找答案。我曾尝试自己做,但我开始相信有一种我不完全理解的机制。我无法解决这个问题!
edit2:我误用了一些词!对于“父母”和“孩子”,我不使用“DOM”的含义!这是用于我当前开发的HTML
<body>
<h1>FIRST LEVEL TITLE</h1>
<h4>test</h4>
<h2>SECOND LEVEL TITLE</h2>
<h3>THIRD LEVEL TITLE</h3>
<h4>test</h4>
<h3>THIRD LEVEL TITLE</h3>
<h3>THIRD LEVEL TITLE</h3>
<h2>SECOND LEVEL TITLE</h2>
<h3>THIRD LEVEL TITLE</h3>
<h3>THIRD LEVEL TITLE</h3>
<h4>test</h4>
<h4>test</h4>
</body>
我想要创建的“父”和“子”层次结构是纯虚拟的(仅存在于我的库中,而不是存在于HTML中)!我的标题标签是不是嵌套
使用Bridge.NET和Bridge.JQuery我已成功从HTML(h1,h2等)中检索完整的标题标签列表,并将它们存储到一个平面列表中。 现在我试图给这个列表一个层次结构,所以我列表中的每个元素都有一个属性“Children”,包含它们正下方的所有元素,这些子元素包含其他子元素......
如果元素与其预期的父元素之间没有中间层元素,则该元素是直接子元素。
列表中的H2,H3,H4,
H4,是H3的孩子,是H2的孩子。 但在列表中 H2,H4,H3,
H3和H4是H2的孩子
示例:
H1 - FIRST LEVEL TITLE
H4 - test
H2 - SECOND LEVEL TITLE
H3 - THIRD LEVEL TITLE
H4 - test
H3 - THIRD LEVEL TITLE
H3 - THIRD LEVEL TITLE
H2 - SECOND LEVEL TITLE
H3 - THIRD LEVEL TITLE
H3 - THIRD LEVEL TITLE
H4 - test
H4 - test
变为
H1 - FIRST LEVEL TITLE
--->H4 - test
--->H2 - SECOND LEVEL TITLE
--->--->H3 - THIRD LEVEL TITLE
--->--->H4 - test
--->--->--->H3 - THIRD LEVEL TITLE
--->--->--->H3 - THIRD LEVEL TITLE
--->H2 - SECOND LEVEL TITLE
--->--->H3 - THIRD LEVEL TITLE
--->--->H3 - THIRD LEVEL TITLE
--->--->H4 - test
--->--->H4 - test
平面列表中的每个标题都定义如下(可以进行任何修改以帮助实现我想要的)
internal class Title
{
public TitleLevel Level { get; set; }
public string Value { get; set; }
public IEnumerable<Title> Children { get; set; }
/* removed irrelevant code */
}
internal enum TitleLevel
{
H6,
H5,
H4,
H3,
H2,
H1,
DummyValue // exists for technical reasons, I may ask for advice on that later
}
答案 0 :(得分:0)
所以这是我想要的解决方案(不平整列表)
代码真的不漂亮,但它有效。
它被称为
var hierarchyList = GetTitleHierarchy(flatList);
这是定义
/// <summary>
/// Turns a flat list of titles into a list where each element contains a list of it children, themselves containing a list of childre, ...
/// </summary>
/// <param name="titles">A flat list of titles</param>
/// <returns>A "cascading" list of titles</returns>
internal List<Title> GetTitleHierarchy(List<Title> titles)
{
return PutInParent(titles);
}
private List<Title> PutInParent(List<Title> titles)
{
var output = new List<Title>();
for (int i = 0; i < titles.Count; i++)
{
// Copy because if passed by reference we'll get loop-referencing
var title = titles.Get(i).Copy();
var childrenCount = CountChildren(titles, i);
if (childrenCount > 0)
{
var subItems = titles.GetRange(i + 1, childrenCount);
title.Children = PutInParent(subItems);
}
output.Add(title);
i += childrenCount;
}
return output;
}
/// <summary>
/// Returns the number of titles after the current index that should be children of the current element
/// </summary>
/// <param name="titles">the flat list of elements</param>
/// <param name="startIndex">the current position in the list</param>
/// <returns></returns>
internal int CountChildren(List<Title> titles, int startIndex)
{
var clidrenCount = 0;
foreach (var title in titles.Skip(startIndex + 1))
{
if (title.IsLevelLowerThan(titles.Get(startIndex).Level))
break;
clidrenCount++;
}
return clidrenCount;
}
internal class Title
{
public TitleLevel Level { get; set; }
public string Value { get; set; }
public IEnumerable<Title> Children { get; set; }
#region Overrides of Object
public override string ToString()
{
return $"{Level} - {Value}";
}
#endregion
public Title Copy()
{
return new Title
{
Level = Level,
Value = Value,
Children = Children.Select(c => c.Copy())
};
}
public bool IsLevelLowerThan(TitleLevel targetLevel)
{
return (int) Level <= (int) targetLevel;
}
}
internal enum TitleLevel
{
H1 = 1,
H2 = 2,
H3 = 3,
H4 = 4,
H5 = 5,
H6 = 6,
DummyValue = 0
}