`
tanglong8848
  • 浏览: 66763 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

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


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics