asp.net读写xml

40 篇文章 0 订阅
  //获取合同信息
            string projectNo = Request.QueryString["projectNo"];

            string Sql="select * from tableName where projectNo='+projectNo+'";
            DAL.DALSelect xx = new DALSelect();
            SqlDataReader reader = xx.ExecuteReader(Sql);

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("/WebTest/DocumentManage/User.xml")); //xml文件路径
            XmlNode xmldocSelect = xmlDoc.SelectSingleNode("User");//xml所有节点的父节点

            XmlNode root = xmlDoc.DocumentElement;
            root.RemoveAll(); //每次写入xml文件的时候都清空原来所有的节点,避免重复的数据写入xml文件

            while (reader.Read())
            {
                string ContractId = reader["ContractId"].ToString();
                string ContractType = reader["ContractType"].ToString();
                string PayMoneyNotId = reader["PayMoneyNotId"].ToString();
                string CustName = reader["CustName"].ToString();
                string PayMoney = reader["PayMoney"].ToString();
                string PayMoneyLimitT = reader["PayMoneyLimitT"].ToString();
                string PayMoneyLimitTUnit = reader["PayMoneyLimitTUnit"].ToString();
                string ContractName = reader["ContractName"].ToString();
                string ContractNo = reader["ContractNo"].ToString();
                string ContractStartT = reader["ContractStartT"].ToString();
                string ContractEndT = reader["ContractEndT"].ToString();
                string ContractRemarks = reader["ContractRemarks"].ToString();



                XmlElement el = xmlDoc.CreateElement("ContractInfo"); //添加ContractInfo节点
                el.SetAttribute("Id", PayMoneyNotId); //添加ContractInfo节点的属性"Id"
                el.SetAttribute("Name", ContractName);   //添加ContractInfo节点的属性 "ContractName"

                XmlElement xesub1 = xmlDoc.CreateElement("ContractType"); //添加ContractInfo节点的里的节点
                xesub1.InnerText = ContractType;//设置文本节点
                el.AppendChild(xesub1);

                XmlElement xesub2 = xmlDoc.CreateElement("PayMoneyNotId");
                xesub2.InnerText = PayMoneyNotId;//设置文本节点
                el.AppendChild(xesub2);

                XmlElement xesub3 = xmlDoc.CreateElement("CustName");
                xesub3.InnerText = CustName;//设置文本节点
                el.AppendChild(xesub3);

                XmlElement xesub4 = xmlDoc.CreateElement("PayMoney");
                xesub4.InnerText = PayMoney;//设置文本节点
                el.AppendChild(xesub4);

                XmlElement xesub5 = xmlDoc.CreateElement("PayMoneyLimitT");
                xesub5.InnerText = PayMoneyLimitT;//设置文本节点
                el.AppendChild(xesub5);

                XmlElement xesub6 = xmlDoc.CreateElement("PayMoneyLimitTUnit");
                xesub6.InnerText = PayMoneyLimitTUnit;//设置文本节点
                el.AppendChild(xesub6);

                XmlElement xesub7 = xmlDoc.CreateElement("ContractName");
                xesub7.InnerText = ContractName;//设置文本节点
                el.AppendChild(xesub7);

                XmlElement xesub8 = xmlDoc.CreateElement("ContractNo");
                xesub8.InnerText = ContractNo;//设置文本节点
                el.AppendChild(xesub8);

                XmlElement xesub9 = xmlDoc.CreateElement("ContractStartT");
                xesub9.InnerText = ContractStartT;//设置文本节点
                el.AppendChild(xesub9);

                XmlElement xesub10 = xmlDoc.CreateElement("ContractEndT");
                xesub10.InnerText = ContractEndT;//设置文本节点
                el.AppendChild(xesub10);

                XmlElement xesub11 = xmlDoc.CreateElement("ContractRemarks");
                xesub11.InnerText = ContractRemarks;//设置文本节点
                el.AppendChild(xesub11);

                XmlElement xesub12 = xmlDoc.CreateElement("ContractId");
                xesub12.InnerText = ContractId;//设置文本节点
                el.AppendChild(xesub12);
                xmldocSelect.AppendChild(el);

                xmlDoc.Save(Server.MapPath("/WebTest/DocumentManage/User.xml")); //保存xml文件
            }
        }


User.xml最初文件

<?xml version="1.0" encoding="utf-8"?>
<User></User>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值