assistant类--xml文件错误信息保存
来源:互联网 发布:淘宝满减凑单再退款 编辑:程序博客网 时间:2024/06/05 23:56
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;
}
}
}
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;
}
}
}
- assistant类--xml文件错误信息保存
- 保存为XML文件
- Xml文件保存聊天记录
- XML文件读取、保存
- Xml文件的保存
- C学习笔记——打开文件,并保存错误信息
- 保存错误信息到本地
- 保存图片到XML文件
- java XML文件的保存
- 15 保存 解析 xml文件
- xml+xsd文件保存配置文件
- xml解析(读取xml,保存文件到xml)
- 如何把DataSet保存为XML文件
- 将图片保存到一个XML文件
- SAP XML输出文件保存到本地
- SAP XML输出文件保存到本地
- java web 上传保存xml文件
- 把DataSet保存为XML文件
- arraylist vector linkedlist
- ASP.NET 网页生命周期&各种事件Event
- 关于channel实现数据的传输
- 适用于连续资源块的数组空闲链表的算法
- 背包问题——解析
- assistant类--xml文件错误信息保存
- VS2010下安装boost库
- 目标跟踪算法compressive tracking的资源
- LightOJ 1011 - Marriage Ceremonies
- Install GCC with an easier way
- 新浪发布首款全媒体覆盖广告平台“龙渊”
- cocos2d-x 通过JNI实现c/c++和Android的java层函数互调
- Linux的bg和fg命令
- ssh的struts注解