判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据

这是我为项目中写的一个测试的例子,

假如,您需要这样一个xml文件,

<?xml version="1.0" encoding="utf-8"?>
<A>
  <a>
    <id>001</id>
    <name>lee</name>
    <time>2013-06-25 16:39:04</time>
  </a>
</A>

如果,本地目录下没有这样的xml文件,就生成一个。。。

如果有,就执行往里面加入数据,如:

<?xml version="1.0" encoding="utf-8"?>
<A>
  <a>
    <id>001</id>
    <name>lee</name>
    <time>2013-06-25 16:39:04</time>
  </a>
<a>
    <id>002</id>
    <name>wan</name>
    <time>2013-06-25 16:40:07</time>
  </a>
<a>
    <id>003</id>
    <name>wang</name>
    <time>2013-06-25 16:48:49</time>
  </a>
... ...
<a>
    <id>00n</id>
    <name>xie</name>
    <time>2013-06-25 16:59:58</time>
  </a>
</A>

其实,我想要的就是这样的效果,以下就是具体的代码实现过程(说明:下面代码实现的结果不是上面的xml,那是我随便写的,大概形式是这样的)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml;
using System.IO;

namespace ThreadExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = "D:\\Click_statistics.xml";
            XmlDocument doc = new XmlDocument();

            if (!File.Exists(path))
            {
                //创建头文件声明
                XmlDeclaration xmldecl;
                xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null);

                //Add the new node to the document.
                XmlElement root = doc.DocumentElement;
                doc.InsertBefore(xmldecl, root);

                XmlElement Node = doc.CreateElement("zhjs");//创建一个zhjs节点          
                doc.AppendChild(Node);

                XmlElement Node1 = doc.CreateElement("total");//创建节点zhjs子节点total   
                doc.DocumentElement.AppendChild(Node1);

                XmlElement Node2 = doc.CreateElement("f_name");//创建节点total子节点f_name 
                Node2.InnerText = "信息查询";
                Node1.AppendChild(Node2);

                XmlElement Node3 = doc.CreateElement("c_name");//创建节点total子节点c_name   
                Node3.InnerText = "";
                Node1.AppendChild(Node3);

                XmlElement Node4 = doc.CreateElement("time");//创建节点total子节点time
                Node4.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Node1.AppendChild(Node4);

                doc.Save(path);     
            }
            else
            {
                doc.Load(path);
                XmlNode xmlnode = doc.SelectSingleNode("zhjs");

                XmlElement Node1 = doc.CreateElement("total");//创建节点zhjs子节点total 
                doc.DocumentElement.AppendChild(Node1);

                XmlElement Node2 = doc.CreateElement("f_name");//创建节点total子节点f_name 
                Node2.InnerText = "公交查询";
                Node1.AppendChild(Node2);

                XmlElement Node3 = doc.CreateElement("c_name");//创建节点total子节点c_name
                Node3.InnerText = "";
                Node1.AppendChild(Node3);

                XmlElement Node4 = doc.CreateElement("time");//创建节点total子节点time
                Node4.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Node1.AppendChild(Node4);

                doc.Save(path);
            }
        }
    }
}


 


<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值