如何使我的浏览器保存来自不同表单的URL

时间:2013-10-28 15:20:00

标签: c# xml url browser

我希望我的网络浏览器将访问过的景点保存到历史记录xml文件中。我有两种历史记录和网络浏览器。我想将url bar文本放入我的历史表单中,我可以使用该方法编写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);
    }

我哪里错了?

0 个答案:

没有答案