assistant类--xml文件错误信息保存

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


namespace windowsServerTest
{
    public class assistant
    {
        public String getXmlAttrValue(String filepath, String nodepath, String attribute, int index)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode(nodepath).ChildNodes;
            XmlElement xe = (XmlElement)nodeList[index];
            String value = xe.GetAttribute(attribute);
            return value;
        }
        public bool appendIfo(String filepath, String date, String message, String stacktrace, String csspath)
        {
            if (!File.Exists(filepath))
            { 
                int i = filepath.LastIndexOf(@"\");
                bool bl = CreateXmlFile(filepath.Substring(0,i-1),filepath.Substring(i+1,filepath.Length-i),csspath);
                if (!bl) return false;
            }
            //使用传统方式添加节点
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filepath);
            XmlNode root = xmlDoc.SelectSingleNode("root");
            XmlElement xe1 = xmlDoc.CreateElement("error");
            
            XmlElement xesub1 = xmlDoc.CreateElement("date");
            xesub1.InnerText = date;
            xe1.AppendChild(xesub1);
            XmlElement xesub2 = xmlDoc.CreateElement("message");
            xesub2.InnerText = message;
            xe1.AppendChild(xesub2);
            XmlElement xesub3 = xmlDoc.CreateElement("stacktrace");
            xesub3.InnerText = stacktrace;
            xe1.AppendChild(xesub3);


            root.AppendChild(xe1);
            xmlDoc.Save(filepath);


            return true;
        }
        public bool CreateXmlFile(String savepath,String filename,String csspath)
        {
            try
            {
                //使用Linq to xml 添加文件头声明
                String target = "xml-stylesheet";
                String data = "href='" + csspath + "'  type='text/css'";
                Object[] param = { new XProcessingInstruction(target, data),new XElement("root") };
                XDocument xdoc = new XDocument(param);
                xdoc.Declaration = new XDeclaration("1.0", "utf-8", "true");


                if (Directory.Exists(savepath))
                    xdoc.Save(savepath + "//" + filename);
                else
                    if(CreateDir(savepath))
                        xdoc.Save(savepath + "//" + filename);
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }
        public static bool CreateDir(String path)
        {
            String[] pf = null;
            String directory = String.Empty;
            try
            {
                if (!File.Exists(path))
                {
                    pf = path.Split(new char[] { '\\' });
                    for (int i = 0; i < pf.Length - 1; i++)
                    {
                        directory += pf[i];
                        if (!Directory.Exists(directory))
                            Directory.CreateDirectory(directory);
                        directory += "\\";
                    }
                }
                return true;
            }
            catch (Exception) {}
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值