0

我希望我的网络浏览器将访问过的景点保存到历史 xml 文件中。我有两种形式的历史和网络浏览器。我想将 url 栏文本放入我的历史记录表单中,我可以在其中编写 xml 文件。

这是我尝试过的。(网络浏览器表单)

 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);
    }

我哪里错了?

4

0 回答 0