C# 读写XML

using System.Xml;
                try
                {
                    //记录访问数据
                    String down_file = "";
                    String count = "";
                    String page = "";
                    XmlDocument doc = new XmlDocument();
                    doc.Load(Server.MapPath("~/cds/news/count.xml"));

                    XmlNodeList xlist = doc.SelectNodes("total");

                    if (xlist.Count == 1)
                    {
                        XmlElement xe = (XmlElement)xlist.Item(0);
                        //循环读取从XML中读到的数据
                        for (int j = 0; j < xe.ChildNodes.Count; j++)
                        {
                            if ((xe.ChildNodes.Item(j).Name) == "page")
                            {
                                page = xe.ChildNodes.Item(j).InnerText;
                            }
                            else if ((xe.ChildNodes.Item(j).Name) == "count_3784")
                            {
                                count = xe.ChildNodes.Item(j).InnerText;
                            }
                            else if ((xe.ChildNodes.Item(j).Name) == "down_file")
                            {
                                down_file = xe.ChildNodes.Item(j).InnerText;
                            }
                        }

                    }



                    //把每个下载累积数+1

                    if (down_file != null && !down_file.Equals(""))
                    {
                        down_file = System.Convert.ToString(Convert.ToInt32(down_file) + 1);
                    }

                    //记录新XML
                    doc.LoadXml("<?xml version='1.0' encoding='utf-8' ?>" + "<total>" + "<page>" + page + "</page>" + "<count_3784>" + count + "</count_3784>" + "<down_file>" + down_file + "</down_file>" + "</total>");

                    //保存新数据
                    doc.Save(Server.MapPath("~/cds/news/count.xml"));




                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw;
                }


你可能感兴趣的:(xml)