using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Xml;
using System.Web;
using System.Data;
using System.IO;
namespace Infarts.BLL
{
public class XmlCommon
{
/// <summary>
/// 删除 单项 + 删除图片
/// </summary>
public static void XmlDeletePhoto(Repeater rep, Page page, System.Web.HttpServerUtility server, string path, string rootnode)
{
int a = 0;
if (File.Exists(server.MapPath(path)))
{
xmldoc.Load(server.MapPath(path));//加载
for (int i = 0; i < rep.Items.Count; i++)
{
CheckBox cbxSelect = (CheckBox)rep.Items[i].FindControl("cbxSelect");
Literal lit = (Literal)rep.Items[i].FindControl("litId");
Literal litPhoto = (Literal)rep.Items[i].FindControl("litPhoto");
if (cbxSelect.Checked)
{
a++;
ShareInfoServices.deletePhotos(server, litPhoto.Text.ToString());//删除图片
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//img_name[text()=" + lit.Text + "]").ParentNode;
node.ParentNode.RemoveChild(node);
xmldoc.Save(server.MapPath((path)));//保存
}
}
}
if (a == 0)
{
ShareInfoServices.alertInfo(page, "请先选中值");
}
else
{
ShareInfoServices.alertInfo(page, "删除成功");
}
}
/// <summary>
/// 删除 单项 无图
/// </summary>
public static void XmlDeleteNoPhoto(Repeater rep, Page page, System.Web.HttpServerUtility server, string path, string rootnode)
{
int a = 0;
if (File.Exists(server.MapPath(path)))
{
xmldoc.Load(server.MapPath(path));//加载
for (int i = 0; i < rep.Items.Count; i++)
{
CheckBox cbxSelect = (CheckBox)rep.Items[i].FindControl("cbxSelect");
Literal lit = (Literal)rep.Items[i].FindControl("litId");
if (cbxSelect.Checked)
{
a++;
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//id[text()=" + lit.Text + "]").ParentNode;
node.ParentNode.RemoveChild(node);
xmldoc.Save(server.MapPath((path)));//保存
}
}
}
if (a == 0)
{
ShareInfoServices.alertInfo(page, "请先选中值", null);
}
else
{
ShareInfoServices.alertInfo(page, "删除成功", null);
}
}
private static XmlDocument xmldoc = new XmlDocument();
#region//xml查询
/// <summary>
/// 读取xml数据
/// </summary>
public static DataSet Bind(string path, HttpServerUtility server)
{
DataSet ds = new DataSet();
if (File.Exists(server.MapPath(path)))
{
ds.ReadXml(server.MapPath(path));
}
return ds;
}
/// <summary>
/// 根据Id获取xml中id的父节点的所有节点 并填充到datatable中
/// </summary>
public static DataTable XmlSelectNodeTextById(HttpServerUtility server, Page page, string path, string rootnode, int id)
{
DataTable dt = new DataTable();
if (File.Exists(server.MapPath(path)))//文件是否存在
{
xmldoc.Load(server.MapPath(path));
XmlNodeList node = xmldoc.SelectSingleNode("" + rootnode + "//img_name[text()=" + id + "]").ParentNode.ChildNodes;
for (int i = 0; i < node.Count; i++)//添加列
{
dt.Columns.Add(node[i].Name, typeof(object));
}
DataRow dr = dt.NewRow();
for (int i = 0; i < node.Count; i++)//添加行
{
dr[i] = node[i].InnerText;
}
dt.Rows.Add(dr);
return dt;
}
else
{
return dt;
}
}
/// <summary>
/// 获取xml中的单个属性值(根据子节点为Id)
/// </summary>
/// <param name="server"></param>
/// <param name="page"></param>
/// <param name="path">路径</param>
/// <param name="id">id</param>
/// <param name="rootnode">根节点</param>
/// <param name="secondnode">根节点下的子节点</param>
/// <param name="selectnode">要获取属性的节点</param>
/// <param name="attributename">属性名</param>
/// <returns></returns>
public static string XmlSelectAttribute(HttpServerUtility server, Page page, string path, int id, string rootnode, string selectnode, string attributename)
{
if (File.Exists(server.MapPath(path)))
{
xmldoc.Load(server.MapPath(path));//加载
string values = xmldoc.SelectSingleNode("" + rootnode + "//id[text()=" + id + "]").ParentNode[selectnode].GetAttribute(attributename);
return values;
}
else
{
ShareInfoServices.alertInfo(page, "更新失败!xml路径指向不存在.", null); return "";
}
}
#endregion
#region//删除
/// <summary>
///xml删除(单项)
/// </summary>
/// <param name="server"></param>
/// <param name="path"></param>
/// <param name="rootnode"></param>
/// <param name="id"></param>
public static void XmlDelete(HttpServerUtility server, string path, string rootnode, string id)
{
if (File.Exists(server.MapPath(path)))//检查是文件否存在
{
xmldoc.Load(server.MapPath(path));//加载
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//id[text()=" + id + "]").ParentNode;
node.ParentNode.RemoveChild(node);
xmldoc.Save(server.MapPath((path)));//保存
}
}
/// <summary>
///xml删除(多项)
/// </summary>
/// <param name="server"></param>
/// <param name="path"></param>
/// <param name="rootnode"></param>
/// <param name="id"></param>
public static void XmlDeleteBatch(HttpServerUtility server, string path, string rootnode, string id)
{
if (File.Exists(server.MapPath(path)))//检查是文件否存在
{
xmldoc.Load(server.MapPath(path));//加载
string[] iid = id.Split(',');
foreach (string iiid in iid)
{
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//id[text()=" + iiid + "]").ParentNode;
node.ParentNode.RemoveChild(node);
}
xmldoc.Save(server.MapPath((path)));//保存
}
}
#endregion
#region//添加xml
/// <summary>
/// xml添加(无属性 只有节点)
/// </summary>
/// <param name="rootnode">根节点</param>
/// <param name="firstnode">二级节点</param>
/// <param name="nodetext">三级节点值</param>
/// <param name="nodename">三级节点名</param>
public static bool XmlAdd(HttpServerUtility server, string path, string rootnode, string firstnode, string[] nodename,string[] nodetext)
{
if (File.Exists(server.MapPath(path))) //文件是否存在
{
xmldoc.Load(server.MapPath(path));//加载
//根节点
XmlNode root = xmldoc.SelectSingleNode(rootnode);
//创建根节点下的子节点
XmlElement xml0 = xmldoc.CreateElement(firstnode);
//添加id节点
int id = 0;
if (root.FirstChild!= null)
{
id = Convert.ToInt32(root.FirstChild["img_name"].InnerText);
}
id = id > 0 ? (id + 1) : 1;
XmlElement xml1 = xmldoc.CreateElement("img_name");
XmlCDataSection xcd1 = xmldoc.CreateCDataSection(id.ToString());
xml1.AppendChild(xcd1);
xml0.AppendChild(xml1);
//循环添加节点
for (int i = 0; i < nodetext.Length; i++)
{
XmlElement xml2 = xmldoc.CreateElement(nodename[i]);
xml2.AppendChild(xmldoc.CreateCDataSection(nodetext[i]));
xml0.AppendChild(xml2);//添加
}
root.InsertBefore(xml0, root.FirstChild);//插入到最前
xmldoc.Save(server.MapPath(path));//保存
return true;
}
else
{
return false;
}
}
#endregion
#region//更新xml
/// <summary>
///更新xml中的节点值(多项)
/// </summary>
/// <param name="rootnode">根节点</param>
/// <param name="id">id</param>
/// <param name="nodename">更新节点的名称</param>
/// <param name="nodetext">更新节点的值</param>
public static bool XmlUpdateNode(HttpServerUtility server, string path, string rootnode, string id, string[] nodename, string[] nodetext)
{
if (File.Exists(server.MapPath(path)))//检查文件是否存在
{
xmldoc.Load(server.MapPath(path));//加载
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//img_name[text()=" + id + "]").ParentNode;
XmlElement xmle = (XmlElement)node;
for (int i = 0; i < nodetext.Length; i++)
{
XmlCDataSection xmlc = xmldoc.CreateCDataSection(nodetext[i]);//cdata
xmle[nodename[i]].InnerText = string.Empty;
xmle[nodename[i]].AppendChild(xmlc);
}
xmldoc.Save(server.MapPath((path)));//保存
return true;
}
else
{
return false;
}
}
/// <summary>
/// 更新xml节点(单项)
/// </summary>
/// <param name="page"></param>
/// <param name="server"></param>
/// <param name="path"></param>
/// <param name="rootnode">根节点</param>
/// <param name="id">id</param>
/// <param name="nodename">更新节点的名称</param>
/// <param name="nodetext">更新节点的值</param>
public static bool XmlUpdateNode(HttpServerUtility server, string path, string rootnode, string id, string nodename, string nodetext)
{
if (File.Exists(server.MapPath(path)))//检查文件是否存在
{
xmldoc.Load(server.MapPath(path));//加载
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//img_name[text()=" + id + "]").ParentNode;
XmlElement xmle = (XmlElement)node;
XmlCDataSection xmlc = xmldoc.CreateCDataSection(nodetext);//cdata
xmle[nodename].InnerText = string.Empty;
xmle[nodename].AppendChild(xmlc);//添加值
xmldoc.Save(server.MapPath((path)));//保存
return true;
}
else
{
return false;
}
}
/// <summary>
/// 更新xml某个节点属性(多项)
/// </summary>
/// <param name="page"></param>
/// <param name="server"></param>
/// <param name="path">xml路径</param>
/// <param name="rootnode">根节点</param>
/// <param name="updatenode">属性所属节点</param>
/// <param name="attributename">属性名</param>
/// <param name="attributevalues">属性值</param>
/// <param name="id"></param>
public static void XmlUpdateAttribute(Page page, HttpServerUtility server, string path, string rootnode, string updatenode, string[] attributename, string[] attributevalues, string id)
{
if (File.Exists(server.MapPath(path)))//检查文件是否存在
{
xmldoc.Load(server.MapPath(path));//加载
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//id[text()=" + id + "]").ParentNode[updatenode];
XmlElement xmle = (XmlElement)node;
for (int i = 0; i < attributevalues.Length; i++)
{
xmle.SetAttribute(attributename[i], attributevalues[i]);
}
xmldoc.Save(server.MapPath((path)));//保存
}
else
{
ShareInfoServices.alertInfo(page, "更新失败!xml路径指向不存在.", null);
}
}
/// <summary>
/// 更新xml某个节点属性(多项)
/// </summary>
/// <param name="page"></param>
/// <param name="server"></param>
/// <param name="path">xml路径</param>
/// <param name="rootnode">根节点</param>
/// <param name="updatenode">属性所属节点</param>
/// <param name="attributename">属性名</param>
/// <param name="attributevalues">属性值</param>
/// <param name="id"></param>
public static void XmlUpdateAttribute(Page page, HttpServerUtility server, string path, string rootnode, string updatenode, string attributename, string attributevalues, string id)
{
if (File.Exists(server.MapPath(path)))//检查文件是否存在
{
xmldoc.Load(server.MapPath(path));//加载
XmlNode node = xmldoc.SelectSingleNode("" + rootnode + "//id[text()=" + id + "]").ParentNode[updatenode];
XmlElement xmle = (XmlElement)node;
xmle.SetAttribute(attributename, attributevalues);
xmldoc.Save(server.MapPath((path)));//保存
}
else
{
ShareInfoServices.alertInfo(page, "更新失败!xml路径指向不存在.", null);
}
}
#endregion
}
}
浙公网安备 33010602011771号