首页 > 代码库 > XML常用操作

XML常用操作

  C#操作XML非常简单

一是写入:

生明一个xelment 再在它的父节点Add就可以,也可以用生明的节点.SetAttribute("节点名称","节点对应的值");

二是读取:

一样也是先生明再得到其子节点再
                    if (xe.Name == "Contrast")
                   
                        if (xe.GetAttribute("IsSelect") == "true")

     即再根据其节点名称 得节点,根据节点属性获取属性值。

 /// <summary>
        /// 写操作日志
        /// </summary>
        /// <param name="logInfo">要写入日志的内容</param>
        /// <param name="flagOfResult">成功还是失败</param>
        public static void WriteLog1(this string logInfo, int flagOfResult)
        {
            XmlDocument doc = new XmlDocument();
            string path = System.Environment.CurrentDirectory + "\\Log.xml";
            doc.Load(path);
            //查找根节点
            XmlNode root = doc.SelectSingleNode("Logs");
            //创建一个DataInfos节点
            XmlElement xe = doc.CreateElement("Log");

            XmlElement operater = doc.CreateElement("操作人");
            operater.InnerText = "Admin";
            xe.AppendChild(operater);
            XmlElement OprateResult = doc.CreateElement("结果");
            OprateResult.InnerText = flagOfResult == 1 ? "成功" : "失败";
            xe.AppendChild(OprateResult);
            XmlElement OprateMsg = doc.CreateElement("结果信息");
            OprateMsg.InnerText = logInfo;
            xe.AppendChild(OprateMsg);
            XmlElement OprateTime = doc.CreateElement("操作时间");
            OprateTime.InnerText = DateTime.Now.ToShortDateString();
            xe.AppendChild(OprateTime);
            root.AppendChild(xe);
            doc.Save(path);


        }
View Code

 

#region  写操作日志   这种格式  
              //      <Log>
              //<操作人>Admin</操作人>
              //<结果>成功</结果>
              //<结果信息>数据导出成功!</结果信息>
              //<操作时间>2014/5/27</操作时间>
              //</Log>


        /// <summary>
        /// 写操作日志
        /// </summary>
        /// <param name="logInfo">要写入日志的内容</param>
        /// <param name="flagOfResult">成功还是失败</param>
        public static void WriteLog1(this string logInfo, int flagOfResult)
        {
            XmlDocument doc = new XmlDocument();
            string path = System.Environment.CurrentDirectory + "\\Log.xml";
            doc.Load(path);
            //查找根节点
            XmlNode root = doc.SelectSingleNode("Logs");
            //创建一个DataInfos节点
            XmlElement xe = doc.CreateElement("Log");

            XmlElement operater = doc.CreateElement("操作人");
            operater.InnerText = "Admin";
            xe.AppendChild(operater);
            XmlElement OprateResult = doc.CreateElement("结果");
            OprateResult.InnerText = flagOfResult == 1 ? "成功" : "失败";
            xe.AppendChild(OprateResult);
            XmlElement OprateMsg = doc.CreateElement("结果信息");
            OprateMsg.InnerText = logInfo;
            xe.AppendChild(OprateMsg);
            XmlElement OprateTime = doc.CreateElement("操作时间");
            OprateTime.InnerText = DateTime.Now.ToShortDateString();
            xe.AppendChild(OprateTime);
            root.AppendChild(xe);
            doc.Save(path);


        }


        #endregion



        #region  写操作日志----------------这种格式     <Log 操作人="Admin" 操作结果="成功" 结果详情="数据导出成功!" 操作时间="2014/5/2714:37" />


        /// <summary>
        /// 写操作日志
        /// </summary>
        /// <param name="logInfo">要写入日志的内容</param>
        /// <param name="flagOfResult">成功还是失败</param>
        public static void WriteLog(this string logInfo, int flagOfResult)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                string path = System.Environment.CurrentDirectory + "\\Log.xml";
                doc.Load(path);
                //查找根节点
                XmlNode root = doc.SelectSingleNode("Logs");
                //创建一个DataInfos节点
                XmlElement xe = doc.CreateElement("Log");
                xe.SetAttribute("操作人", "Admin");
                xe.SetAttribute("操作结果", flagOfResult == 1 ? "成功" : "失败");
                xe.SetAttribute("结果详情", logInfo);
                xe.SetAttribute("操作时间", DateTime.Now.ToShortDateString() + DateTime.Now.ToShortTimeString());
                root.AppendChild(xe);
                doc.Save(path);
            }
            catch (Exception)
            {

                return;
            }



        }


        #endregion

 

 

#region   根据配置文件初始化SQL语句

        /// <summary>
        /// 根据表名及其配置文件生成T_SQL语句及要生成的Excel的表头
        /// </summary>
        /// <param name="TableName">要查询的Table的表名</param>
        /// <returns>正常:返回List<string>表头String及T_SqLString;异常:返回单个以“错误”+异常信息的List</returns>
        public static List<string> InitSqlString(this string TableName)
        {
            List<string> listResult = new List<string>();
            try
            {
                StringBuilder sbOfT_SQL = new StringBuilder();
                StringBuilder sbOfExcelHeadName = new StringBuilder();
                sbOfT_SQL.Append("select ");
                XmlDocument doc = new XmlDocument();
                doc.Load(System.Environment.CurrentDirectory + "\\TableDetailInfo\\" + TableName + ".xml");
                XmlNodeList xmlList = doc.SelectSingleNode("ContrastList").ChildNodes;
                foreach (XmlNode xn in xmlList)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.Name == "Contrast")
                    {
                        if (xe.GetAttribute("IsSelect") == "true")
                        {
                            sbOfT_SQL.Append(xe.GetAttribute("DestField"));
                            sbOfExcelHeadName.Append(xe.GetAttribute("SourceField"));
                            sbOfExcelHeadName.Append(",");
                            sbOfT_SQL.Append(",");
                        }

                    }
                }
                sbOfT_SQL.Remove(sbOfT_SQL.ToString().Length - 1, 1);
                sbOfExcelHeadName.Remove(sbOfExcelHeadName.ToString().Length - 1, 1);
                sbOfT_SQL.Append(" from " + TableName);
                listResult.Add(sbOfExcelHeadName.ToString());
                listResult.Add(sbOfT_SQL.ToString());


            }
            catch (Exception ex)
            {
                listResult.RemoveRange(0, listResult.Count);
                listResult.Add("错误");
            }
            return listResult;
        }

        #endregion