我想使用“BookmarksManager.Chrome”(NuGet包)。 我已经能够使用ChromeBookmarksReader.Read阅读书签。但我需要在树中显示书签结构,而我还没有找到一种方法来获取书签ID和parentid。 有谁知道如何做到这一点? ChromeBookmarkModel似乎拥有所有信息。但是怎么用呢?无法将BookmarkFolder转换为ChromeBookmarkModel ...
最好的问候,汤姆
答案 0 :(得分:0)
我建议使用Html Agility Pack代替BookmarksManager nuget。 我尝试实现与您相同的功能,但使用BookmarksManager nuget没有运气。幸运的是,我能够找到一种使用Html Agility Pack显示文件夹层次结构的方法。不可否认,我的解决方案是针对WPF的,但我确定将其重写为WinForms并不是什么大不了的事。
<强> 1。使用Html Agility Pack删除所有阻止文件成为有效XML的标记,从而阻止我们正确阅读结构:
public void OpenBookmarksFile()
{
string BookmarksFile_w_impeding_tags = @"your_bookmarks_file.html";
BuildTree(treeView, XDocument.Load(Path.Combine(Directory.GetCurrentDirectory(), (DeleteTags(BookmarksFile_w_impeding_tags)))));
}
public string DeleteTags(string BookmarksFile_w_impeding_tags)
{
StreamReader BookmarkDatei = new StreamReader(BookmarksFile_w_impeding_tags);
string content = BookmarkDatei.ReadToEnd();
BookmarkDatei.Close();
HtmlDocument doc_lessTags = new HtmlDocument();
//deletes all DD Tags
string DD = "(<DD>[a-zA-Z0-9]+[^<]+)"; //Regex-Pattern
doc_lessTags.LoadHtml(Regex.Replace(content, DD, ""));
//variable for each tag that could be impeding for displaying the correct hierarchy
var metas = doc_lessTags.DocumentNode.SelectNodes("//meta");
var titles = doc_lessTags.DocumentNode.SelectNodes("//title");
var h1s = doc_lessTags.DocumentNode.SelectNodes("//h1");
var dts = doc_lessTags.DocumentNode.SelectNodes("//dt");
var ps = doc_lessTags.DocumentNode.SelectNodes("//p");
var hrs = doc_lessTags.DocumentNode.SelectNodes("//hr");
var dds = doc_lessTags.DocumentNode.SelectNodes("//dd");
var aa = doc_lessTags.DocumentNode.SelectNodes("//a");
var h3s = doc_lessTags.DocumentNode.SelectNodes("//h3");
//delete all tags that could be impeding (comments too)
//------------------------------------------------------------------------------------------------------------------------
var doctype = doc_lessTags.DocumentNode.SelectSingleNode("/comment()[starts-with(.,'<!DOCTYPE')]");
if (doctype != null)
{
doctype.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
var comments = doc_lessTags.DocumentNode.SelectSingleNode("//comment()");
if (comments != null)
{
comments.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
foreach (var meta in metas)
{
meta.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
foreach (var title in titles)
{
title.Remove();
}
//------------------------------------------------------------------------------------------------------------------------
foreach (var h1 in h1s)
{
h1.Remove();
}
//-----some(open tags, like DT) can not be deleted the normal way becaue they would reverse the order of our file------
if (dts != null)
{
foreach (var dt in dts)
{
if (!dt.HasChildNodes)
{
dt.ParentNode.RemoveChild(dt);
continue;
}
for (var i = dt.ChildNodes.Count - 1; i >= 0; i--)
{
var child = dt.ChildNodes[i];
dt.ParentNode.InsertAfter(child, dt);
}
dt.ParentNode.RemoveChild(dt);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------
if (ps != null)
{
foreach (var p in ps)
{
if (!p.HasChildNodes)
{
p.ParentNode.RemoveChild(p);
continue;
}
for (var i = p.ChildNodes.Count - 1; i >= 0; i--)
{
var child = p.ChildNodes[i];
p.ParentNode.InsertAfter(child, p);
}
p.ParentNode.RemoveChild(p);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------
if (hrs != null)
{
foreach (var hr in hrs)
{
if (!hr.HasChildNodes)
{
hr.ParentNode.RemoveChild(hr);
continue;
}
for (var i = hr.ChildNodes.Count - 1; i >= 0; i--)
{
var child = hr.ChildNodes[i];
hr.ParentNode.InsertAfter(child, hr);
}
hr.ParentNode.RemoveChild(hr);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------
if (dds != null)
{
foreach (var dd in dds)
{
if (!dd.HasChildNodes)
{
dd.ParentNode.RemoveChild(dd);
continue;
}
for (var i = dd.ChildNodes.Count - 1; i >= 0; i--)
{
var child = dd.ChildNodes[i];
dd.ParentNode.InsertAfter(child, dd);
}
dd.ParentNode.RemoveChild(dd);
}
}
//------------------------------------------------------------------------------------------------------------------------
//system partition
string sysPart = System.IO.Path.GetPathRoot(Environment.SystemDirectory);
//currently logged in user
string userName = Environment.UserName;
//after deleting all impeding tags we save the result to a new file so that we can keep our original file untouched
string BookmarksFile_less_tags = $@"{sysPart}Users\{userName}\Desktop\bookmarks_less_tags";
doc_lessTags.Save(BookmarksFile_less_tags);
return BookmarksFile_less_tags;
}
<强> 2。使用新创建的File with System.Xml;和System.Xml.Linq;填充TreeView
public void BuildTree(TreeView treeView, XDocument doc)
{
TreeViewItem treeNode = new TreeViewItem
{
//Should be Root
Header = doc.Root.Name.LocalName,
IsExpanded = true
};
treeView.Items.Add(treeNode);
BuildNodes(treeNode, doc.Root);
}
public void BuildNodes(TreeViewItem treeNode, XElement element)
{
foreach (XNode child in element.Nodes())
{
switch (child.NodeType)
{
case XmlNodeType.Element:
XElement childElement = child as XElement;
XElement prev = child.PreviousNode as XElement;
if(childElement.Name == "dl" && prev.Name == "h3")
{
TreeViewItem childTreeNode = new TreeViewItem
{
//because only the dl nodes have child nodes we want
//to display the dl nodes with the inner text of thier respective previous node
Header = prev.Value
};
treeNode.Items.Add(childTreeNode);
BuildNodes(childTreeNode, childElement);
}
break;
case XmlNodeType.Text:
XText childText = child as XText;
treeNode.Items.Add(new TreeViewItem { Header = childText.Value, });
break;
}
}
}
if you don't add any icons it will look something like this..
答案 1 :(得分:0)
文档不是很详细。
但是,代码足够小,可以(部分)替换文档:
public class BookmarkFolder : Collection<IBookmarkItem>, IBookmarkFolder
换句话说:您自己获取的每个文件夹 包含其子项(文件夹和链接)的集合:
using (var file = File.OpenRead(path)) {
var reader = new NetscapeBookmarksReader();
var bookmarks = reader.Read(file);
foreach (var item in bookmarks) {
if (item is BookmarkFolder) {
} else if (item is BookmarkLink) {
}
....
请参阅https://github.com/Dissimilis/BookmarksManager/blob/master/BookmarksManager/BookmarkFolder.cs#L8