Web ブラウザーで訪れた場所を履歴 xml ファイルに保存したいと考えています。履歴用と Web ブラウザー用の 2 つのフォームがあります。xml ファイルを書き込む方法がある履歴フォームに URL バーのテキストを取得したいと思います。
これは私が試したものです。(ウェブブラウザフォーム)
public String historyURL
{
get
{
return urlBar.Text;
}
そして、履歴フォームでこのメソッドを呼び出そうとしました
private void addHistory(string url, string data)
{
url = this.WebBrowserForm.historyURL.ToString();
XmlDocument myXml = new XmlDocument();
int i=1;
XmlElement el = myXml.CreateElement("item");
el.SetAttribute("url", url);
el.SetAttribute("lastVisited", data);
私の showHistory メソッド
private void showHistory()
{
historyTreeView.Nodes.Clear();
XmlDocument myXml = new XmlDocument();
if (File.Exists(historyXml))
{
myXml.Load(historyXml);
DateTime now = DateTime.Now;
if (sortHistory.Text.Equals("Ordered Visited Today"))
{
historyTreeView.ShowRootLines = false;
foreach (XmlElement el in myXml.DocumentElement.ChildNodes)
{
DateTime d = DateTime.Parse(el.GetAttribute("lastVisited"));
if (!(d.Date == now.Date)) return;
TreeNode node =
new TreeNode(el.GetAttribute("url"), 3, 3);
node.ToolTipText = el.GetAttribute("url") + "\nLast Visited: " + el.GetAttribute("lastVisited") + "\nTimes Visited: " + el.GetAttribute("times");
node.Name = el.GetAttribute("url");
node.ContextMenuStrip = histContextMenu;
historyTreeView.Nodes.Add(node);
}
}
if (sortHistory.Text.Equals("View by Date"))
{
historyTreeView.ShowRootLines = true;
historyTreeView.Nodes.Add("2 Weeks Ago", "2 Weeks Ago", 2, 2);
historyTreeView.Nodes.Add("Last Week", "Last Week", 2, 2);
historyTreeView.Nodes.Add("This Week", "This Week", 2, 2);
historyTreeView.Nodes.Add("Yesterday", "Yesterday", 2, 2);
historyTreeView.Nodes.Add("Today", "Today", 2, 2);
foreach (XmlElement el in myXml.DocumentElement.ChildNodes)
{
DateTime d = DateTime.Parse(el.GetAttribute("lastVisited"));
TreeNode node = new TreeNode(el.GetAttribute("url"), 3, 3);
node.ToolTipText = el.GetAttribute("url") + "\nLast Visited: " + el.GetAttribute("lastVisited") + "\nTimes Visited: " + el.GetAttribute("times");
node.Name = el.GetAttribute("url");
node.ContextMenuStrip = histContextMenu;
if (d.Date == now.Date)
historyTreeView.Nodes[4].Nodes.Add(node);
else
if (d.AddDays(1).ToShortDateString().Equals(now.ToShortDateString()))
historyTreeView.Nodes[3].Nodes.Add(node);
else
if (d.AddDays(7) > now)
historyTreeView.Nodes[2].Nodes.Add(node);
else
if (d.AddDays(14) > now)
historyTreeView.Nodes[1].Nodes.Add(node);
else
if (d.AddDays(21) > now)
historyTreeView.Nodes[0].Nodes.Add(node);
else
if (d.AddDays(22) > now)
myXml.DocumentElement.RemoveChild(el);
}
参照用の完全な addHistory メソッド
private void addHistory(string url, string data)
{
url = this.WebBrowserForm.historyURL.ToString();
XmlDocument myXml = new XmlDocument();
int i=1;
XmlElement el = myXml.CreateElement("item");
el.SetAttribute("url", url);
el.SetAttribute("lastVisited", data);
if (!File.Exists(historyXml))
{
XmlElement root = myXml.CreateElement("history");
myXml.AppendChild(root);
el.SetAttribute("times", "1");
root.AppendChild(el);
}
else
{
myXml.Load(historyXml);
foreach (XmlElement x in myXml.DocumentElement.ChildNodes)
{
if (x.GetAttribute("url").Equals(url))
{
i = int.Parse(x.GetAttribute("times")) + 1;
myXml.DocumentElement.RemoveChild(x);
break;
}
}
el.SetAttribute("times", i.ToString());
myXml.DocumentElement.InsertBefore(el, myXml.DocumentElement.FirstChild);
/*ordered visited today*/
if (sortHistory.Text.Equals("Ordered Visited Today"))
{
if (!historyTreeView.Nodes.ContainsKey(url))
{
TreeNode node =
new TreeNode(url, 3, 3);
node.ToolTipText = url + "\nLast Visited: " + data + "\nTimes visited :" + i.ToString();
node.Name = url.ToString();
node.ContextMenuStrip = histContextMenu;
historyTreeView.Nodes.Insert(0, node);
}
else
historyTreeView.Nodes[url].ToolTipText
= url + "\nLast Visited: " + data + "\nTimes visited: " + i.ToString();
}
/* view by date*/
if (sortHistory.Text.Equals("View by Date"))
{
if (historyTreeView.Nodes[4].Nodes.ContainsKey(url))
historyTreeView.Nodes[url].ToolTipText
= url.ToString() + "\nLast Visited: " + data + "\nTimes visited: " + i.ToString();
else
{
TreeNode node =
new TreeNode(url, 3, 3);
node.ToolTipText = url + "\nLast Visited: " + data + "\nTimes visited :" + i.ToString();
node.Name = url;
node.ContextMenuStrip = histContextMenu;
historyTreeView.Nodes[4].Nodes.Add(node);
}
}
}
myXml.Save(historyXml);
}
どこが間違っていますか?