自己写的asp 创建修改 xml的类 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Xml; using System.Collections; using System.IO; namespace PartyMis { /// <summary> /// XmlBase 的摘要说明 /// 对XML操作的一些小功能 /// /// 2009-12-22 肖松茂制作 /// ,转载请保留 /// 2009-12-26 重大修改 /// 1.修改了TableToXml 方法,删除SqlToXml 方法 /// 2.增加删除,修改操作。 /// 3.添加数据可以只添加一行,不需要整张表--InsertXml方法 /// /// Email: smx1989@gmail.com /// </summary> public class XmlBase { public XmlBase() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 把给出的表存放到一个新的xml文件中 /// 如果源文件已经存在,替换; /// </summary> /// <param name="dt">给出的数据表,最好设置了TableName</param> /// <param name="strPath">xml文件存放位置,包括文件名</param> /// <returns>true|false</returns> public static bool TableToXml(DataTable dt, string strPath) { //string sql = "SELECT id,编码类型,编码名称 FROM T_Code"; try { DataSet ds = new DataSet(); //截取传入的 xml 文件路径,获得文件名 string strName = strPath.Substring(strPath.LastIndexOf('//') + 1); //设置DataSet name 属性 ds.DataSetName = strName; //把表放入DataSet ds.Tables.Add(dt); //执行写入方法,参数是xml文件路径 if(File.Exists(strPath)) File.Delete(strPath); ds.WriteXml(strPath); return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 把指定的xml文件读入到一张表里 /// </summary> /// <param name="path">xml 文件的路径</param> /// <returns>生成的表</returns> public static DataTable XmlToTable(string path) { DataSet ds = new DataSet(); //执行DataSet 的方法,读取xml文件里的数据,生成一张表 try { ds.ReadXml(path); DataTable dt = ds.Tables[0]; return dt; } catch (Exception e1) { Resport.WriteToLog(e1); return new DataTable(); } } /// <summary> /// 向指定的xml文件增加节点、数据,一次只插入一条--没有节点属性 /// </summary> /// <param name="path">xml文件路径,包括文件名</param> /// <param name="rootNode">查找顶级节点的名称</param> /// <param name="node">设置插入的节点名</param> /// <param name="nodeName">设置插入的节点的数据名</param> /// <param name="nodeValue">设置插入的节点的数据值</param> /// <returns></returns> public static bool InsertXml(string path, string rootNode, string node, string[] nodeName, ArrayList nodeValue) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNode root = xmlDoc.SelectSingleNode(rootNode);//查找rootNode 根节点 XmlElement xe = xmlDoc.CreateElement(node);//创建一个 node 节点 for (int j = 0; j < nodeName.Length; j++) { XmlElement xeChild = xmlDoc.CreateElement(nodeName[j]);//新建 node 节点的子节点 xeChild.InnerText = nodeValue[j].ToString();//设置文本节点 xe.AppendChild(xeChild);//添加到 node 节点中 } root.AppendChild(xe);//添加到 rootNode 节点中 xmlDoc.Save(path); return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 向指定的xml文件增加节点、数据,一次只插入一条--有节点属性 /// </summary> /// <param name="path">xml文件路径,包括文件名</param> /// <param name="rootNode">查找顶级节点的名称</param> /// <param name="node">设置插入的节点名</param> /// <param name="attriName">设置插入的节点的属性名</param> /// <param name="attriValue">设置插入的节点的属性值</param> /// <param name="nodeName">设置插入的节点的数据名--被当做子节点</param> /// <param name="nodeValue">设置插入的节点的数据值--被当做子节点</param> /// <returns>true|false</returns> public static bool InsertXml(string path, string rootNode, string node, string[] attriName,ArrayList attriValue,string[] nodeName,ArrayList nodeValue) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNode root = xmlDoc.SelectSingleNode(rootNode);//查找rootNode 根节点 XmlElement xe = xmlDoc.CreateElement(node);//创建一个 node 节点 for (int i = 0; i < attriName.Length; i++) { xe.SetAttribute(attriName[i], attriValue[i].ToString());//设置该节点属性 } for (int j = 0; j < nodeName.Length; j++) { XmlElement xeChild = xmlDoc.CreateElement(nodeName[j]);//新建 node 节点的子节点 xeChild.InnerText = nodeValue[j].ToString();//设置文本节点 xe.AppendChild(xeChild);//添加到 node 节点中 } root.AppendChild(xe);//添加到 rootNode 节点中 xmlDoc.Save(path); return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 修改指定xml文件的指定节点--有节点属性 /// 注意!第一个节点属性用来查找节点!例如: /// 修改《book id="001"》 的节点:attriName[0]=id attriValue[0]=001;--用来找到这个节点 /// attriName[1]=id attriValue[1]=002 --用来修改数据,第1个以后的都是用来修改数据的 /// </summary> /// <param name="path">xml文件的路径</param> /// <param name="rootNode">顶级节点名</param> /// <param name="node">要修改的节点名</param> /// <param name="attriName">要修改的节点的属性名</param> /// <param name="attriValue">要修改的节点的属性值</param> /// <param name="nodeName">要修改的数据节点名</param> /// <param name="nodeValue">要修改的数据节点值</param> /// <returns></returns> public static bool UpdateXml(string path, string rootNode, string node, string[] attriName, ArrayList attriValue, string[] nodeName, ArrayList nodeValue) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNodeList nodeList = xmlDoc.SelectSingleNode(rootNode).ChildNodes;//获取rootNode节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute(attriName[0]) == attriValue[0].ToString())//根据 节点的 属性 查找节点 { for (int i = 0; i < attriName.Length; i++) { xe.SetAttribute(attriName[i], attriValue[i].ToString());//修改该节点的相应属性值 } for (int j = 0; j < nodeName.Length; j++) { XmlNode xn1 = xe.SelectSingleNode(nodeName[j]); xn1.InnerText = nodeValue[j].ToString();//修改该节点下的对应内容 } break; } } xmlDoc.Save(path);//保存。 return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 修改指定xml文件的指定节点--没有节点属性 /// 注意!第一个数据节点用来查找节点!例如: /// 修改 id=01 的数据节点 修改成 id=02, nodeName[0]=id nodeValue[0]=01;nodename[1]=id,nodeValue[1]=02 /// </summary> /// <param name="path">xml文件的路径</param> /// <param name="rootNode">顶级节点名</param> /// <param name="node">要修改的节点名</param> /// <param name="nodeName">要修改的数据节点名</param> /// <param name="nodeValue">要修改的数据节点值</param> /// <returns></returns> public static bool UpdateXml(string path, string rootNode, string node, string[] nodeName, ArrayList nodeValue) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNodeList nodeList = xmlDoc.SelectSingleNode(rootNode).ChildNodes;//获取rootNode节点的所有子节点 table1 foreach (XmlNode xn in nodeList)//遍历所有子节点 { bool blnFind = false; XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 XmlNodeList nodeList1 = xe.ChildNodes;//获取 xe 节点的所有子节点 id,编码,类型 foreach (XmlNode xn1 in nodeList1)//遍历所有子节点 { XmlElement xe1 = (XmlElement)xn1;//将子节点类型转换为XmlElement类型 if (xe1.Name == nodeName[0] && xe1.InnerText == nodeValue[0].ToString())//根据 节点的 名字 查找节点 "id" && "2" { blnFind = true; break; } } if (blnFind) { for (int i = 1; i < nodeName.Length; i++) { XmlNode xn2 = xe.SelectSingleNode(nodeName[i]); xn2.InnerText = nodeValue[i].ToString(); } } } xmlDoc.Save(path);//保存。 return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 删除指定节点--有属性 /// </summary> /// <param name="path">xml文件的路径</param> /// <param name="rootNode">顶级节点名</param> /// <param name="node">要删除的节点名</param> /// <param name="attriName">要删除的节点的属性名</param> /// <param name="attriValue">要删除的节点的属性值</param> /// <returns>true | false</returns> public static bool DeleteXml(string path, string rootNode, string node, string attriName, string attriValue) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNode rootnode = xmlDoc.SelectSingleNode(rootNode); XmlNodeList nodeList = rootnode.ChildNodes;//获取rootNode节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 if (xe.GetAttribute(attriName) == attriValue)//根据 节点的 属性 查找节点 { rootnode.RemoveChild(xe); break; } } xmlDoc.Save(path);//保存。 return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 删除指定节点或数据,--没有属性 /// </summary> /// <param name="path">xml文件的路径</param> /// <param name="rootNode">顶级节点名</param> /// <param name="node">要删除的节点名</param> /// <param name="nodeName">要删除的节点的数据节点名</param> /// <param name="nodeValue">要删除的节点的数据节点值</param> /// <param name="all">是否把整个节点删除,否,只删除节点中上面的数据</param> /// <returns>true | false</returns> public static bool DeleteXml(string path, string rootNode, string node, string nodeName, string nodeValue,bool all) { try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); XmlNode rootnode = xmlDoc.SelectSingleNode(rootNode); XmlNodeList nodeList = rootnode.ChildNodes;//获取rootNode节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 XmlNodeList nodeList1 = xe.ChildNodes;//获取 xe 节点的所有子节点 id,编码,类型 foreach (XmlNode xn1 in nodeList1)//遍历所有子节点 { XmlElement xe1 = (XmlElement)xn1;//将子节点类型转换为XmlElement类型 if (xe1.Name == nodeName && xe1.InnerText == nodeValue)//根据 节点的 名字 查找节点 "id" && "2" { if (all) rootnode.RemoveChild(xe); else xe.RemoveChild(xe1); break; } } } xmlDoc.Save(path);//保存。 return true; } catch (Exception e1) { Resport.WriteToLog(e1); return false; } } /// <summary> /// 导出成为Excel /// </summary> /// <param name="exportTargetGridView">目标GridView</param> //public static void ExportExcel(GridView exportTargetGridView) //{ // HttpContext.Current.Response.ClearContent(); // HttpContext.Current.Response.Charset = "GB2312"; // HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7; // HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.doc"); // HttpContext.Current.Response.ContentType = "application/word"; // StringWriter sw = new StringWriter(); // HtmlTextWriter htw = new HtmlTextWriter(sw); // exportTargetGridView.RenderControl(htw); // HttpContext.Current.Response.Write(sw.ToString()); // HttpContext.Current.Response.End(); //} } }