xml

//写入XML文件 

        public static void SavetoXml(List<ProxyEntity> proxys)
        {
            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "proxyIP.xml");

            XmlDocument xmlDoc = new XmlDocument();
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);
            XmlNode root = xmlDoc.CreateElement("Proxys");
            xmlDoc.AppendChild(root);

            foreach (var proxy in proxys)
            {
                XmlNode childNode = xmlDoc.CreateElement("Proxy");
                root.AppendChild(childNode);
                AppendChildNode(xmlDoc, childNode, "IP", proxy.IP);
                AppendChildNode(xmlDoc, childNode, "Port", proxy.Port);
                AppendChildNode(xmlDoc, childNode, "CityName", proxy.CityName);
                AppendChildNode(xmlDoc, childNode, "Anonymity", proxy.Anonymity);
                AppendChildNode(xmlDoc, childNode, "RequestType", proxy.RequestType);
            }

            if (File.Exists(path))
            {
                File.Delete(path);
            }

            xmlDoc.Save(path);

        }

 

 

 

 

//读XML文件 

 

public static List<ProxyEntity> ReadProxyToXml(string path)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(path);
            XmlNodeList nodeList = xmlDoc.SelectNodes("//Proxys/Proxy");

            var proxyList = new List<ProxyEntity>();

            foreach (XmlNode node in nodeList)
            {
                var proxy = new ProxyEntity();
                proxy.IP = node.SelectSingleNode("IP").InnerText;
                proxy.Port = node.SelectSingleNode("Port").InnerText;
                proxy.CityName = node.SelectSingleNode("CityName").InnerText;
                proxy.Anonymity = node.SelectSingleNode("Anonymity").InnerText;
                proxy.RequestType = node.SelectSingleNode("RequestType").InnerText;

                proxyList.Add(proxy);
            }

            return proxyList;

        }

 

posted @ 2016-02-24 09:38  Zery  阅读(1309)  评论(0编辑  收藏  举报