123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606 |
- using System;
- using System.Data;
- using System.IO;
- using System.Xml;
- namespace Ant.Service.Utilities
- {
- public class XMLProcess
- {
- #region 构造函数
- public XMLProcess()
- { }
- public XMLProcess(string strPath)
- {
- this._XMLPath = strPath;
- }
- #endregion
- #region 公有属性
- private string _XMLPath;
- public string XMLPath
- {
- get { return this._XMLPath; }
- }
- #endregion
- #region 私有方法
-
-
-
-
- private XmlDocument XMLLoad()
- {
- string XMLFile = XMLPath;
- XmlDocument xmldoc = new XmlDocument();
- try
- {
- string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile;
- if (File.Exists(filename)) xmldoc.Load(filename);
- }
- catch (Exception e)
- { }
- return xmldoc;
- }
-
-
-
-
- private static XmlDocument XMLLoad(string strPath)
- {
- XmlDocument xmldoc = new XmlDocument();
- try
- {
- string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + strPath;
- if (File.Exists(filename)) xmldoc.Load(filename);
- }
- catch (Exception e)
- { }
- return xmldoc;
- }
-
-
-
-
- private static string GetXmlFullPath(string strPath)
- {
- if (strPath.IndexOf(":") > 0)
- {
- return strPath;
- }
- else
- {
- return System.Web.HttpContext.Current.Server.MapPath(strPath);
- }
- }
- #endregion
- #region 读取数据
-
-
-
-
-
-
-
- public string Read(string node)
- {
- string value = "";
- try
- {
- XmlDocument doc = XMLLoad();
- XmlNode xn = doc.SelectSingleNode(node);
- value = xn.InnerText;
- }
- catch { }
- return value;
- }
-
-
-
-
-
-
-
-
-
- public static string Read(string path, string node)
- {
- string value = "";
- try
- {
- XmlDocument doc = XMLLoad(path);
- XmlNode xn = doc.SelectSingleNode(node);
- value = xn.InnerText;
- }
- catch { }
- return value;
- }
-
-
-
-
-
-
-
-
-
- public static string Read(string path, string node, string attribute)
- {
- string value = "";
- try
- {
- XmlDocument doc = XMLLoad(path);
- XmlNode xn = doc.SelectSingleNode(node);
- value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
- }
- catch { }
- return value;
- }
-
-
-
-
- public string[] ReadAllChildallValue(string node)
- {
- int i = 0;
- string[] str = { };
- XmlDocument doc = XMLLoad();
- XmlNode xn = doc.SelectSingleNode(node);
- XmlNodeList nodelist = xn.ChildNodes;
- if (nodelist.Count > 0)
- {
- str = new string[nodelist.Count];
- foreach (XmlElement el in nodelist)
- {
- str[i] = el.Value;
- i++;
- }
- }
- return str;
- }
-
-
-
-
- public XmlNodeList ReadAllChild(string node)
- {
- XmlDocument doc = XMLLoad();
- XmlNode xn = doc.SelectSingleNode(node);
- XmlNodeList nodelist = xn.ChildNodes;
- return nodelist;
- }
-
-
-
-
-
- public DataView GetDataViewByXml(string strWhere, string strSort)
- {
- try
- {
- string XMLFile = this.XMLPath;
- string filename = AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLFile;
- DataSet ds = new DataSet();
- ds.ReadXml(filename);
- DataView dv = new DataView(ds.Tables[0]);
- if (strSort != null)
- {
- dv.Sort = strSort;
- }
- if (strWhere != null)
- {
- dv.RowFilter = strWhere;
- }
- return dv;
- }
- catch (Exception)
- {
- return null;
- }
- }
-
-
-
-
- public DataSet GetDataSetByXml(string strXmlPath)
- {
- try
- {
- DataSet ds = new DataSet();
- ds.ReadXml(GetXmlFullPath(strXmlPath));
- if (ds.Tables.Count > 0)
- {
- return ds;
- }
- return null;
- }
- catch (Exception)
- {
- return null;
- }
- }
- #endregion
- #region 插入数据
-
-
-
-
-
-
-
-
-
-
-
-
- public static void Insert(string path, string node, string element, string attribute, string value)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- XmlNode xn = doc.SelectSingleNode(node);
- if (element.Equals(""))
- {
- if (!attribute.Equals(""))
- {
- XmlElement xe = (XmlElement)xn;
- xe.SetAttribute(attribute, value);
- }
- }
- else
- {
- XmlElement xe = doc.CreateElement(element);
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- xn.AppendChild(xe);
- }
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- }
- catch { }
- }
-
-
-
-
-
-
-
- public static void Insert(string path, string node, string element, string[][] strList)
- {
- try
- {
- XmlDocument doc = new XmlDocument();
- doc.Load(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- XmlNode xn = doc.SelectSingleNode(node);
- XmlElement xe = doc.CreateElement(element);
- string strAttribute = "";
- string strValue = "";
- for (int i = 0; i < strList.Length; i++)
- {
- for (int j = 0; j < strList[i].Length; j++)
- {
- if (j == 0)
- strAttribute = strList[i][j];
- else
- strValue = strList[i][j];
- }
- if (strAttribute.Equals(""))
- xe.InnerText = strValue;
- else
- xe.SetAttribute(strAttribute, strValue);
- }
- xn.AppendChild(xe);
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- }
- catch { }
- }
-
-
-
-
-
-
-
- public static bool WriteXmlByDataSet(string strXmlPath, string[] Columns, string[] ColumnValue)
- {
- try
- {
-
- string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd";
- DataSet ds = new DataSet();
- ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));
- ds.ReadXml(GetXmlFullPath(strXmlPath));
- DataTable dt = ds.Tables[0];
- DataRow newRow = dt.NewRow();
- for (int i = 0; i < Columns.Length; i++)
- {
- newRow[Columns[i]] = ColumnValue[i];
- }
- dt.Rows.Add(newRow);
- dt.AcceptChanges();
- ds.AcceptChanges();
- ds.WriteXml(GetXmlFullPath(strXmlPath));
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- #endregion
- #region 修改数据
-
-
-
-
-
- public void Update(string node, string value)
- {
- try
- {
- XmlDocument doc = XMLLoad();
- XmlNode xn = doc.SelectSingleNode(node);
- xn.InnerText = value;
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + XMLPath);
- }
- catch { }
- }
-
-
-
-
-
-
-
-
-
- public static void Update(string path, string node, string value)
- {
- try
- {
- XmlDocument doc = XMLLoad(path);
- XmlNode xn = doc.SelectSingleNode(node);
- xn.InnerText = value;
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- }
- catch { }
- }
-
-
-
-
-
-
-
-
-
-
- public static void Update(string path, string node, string attribute, string value)
- {
- try
- {
- XmlDocument doc = XMLLoad(path);
- XmlNode xn = doc.SelectSingleNode(node);
- XmlElement xe = (XmlElement)xn;
- if (attribute.Equals(""))
- xe.InnerText = value;
- else
- xe.SetAttribute(attribute, value);
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- }
- catch { }
- }
-
-
-
-
-
-
-
-
- public static bool UpdateXmlRow(string strXmlPath, string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue)
- {
- try
- {
- string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd";
- DataSet ds = new DataSet();
- ds.ReadXmlSchema(GetXmlFullPath(strXsdPath));
- ds.ReadXml(GetXmlFullPath(strXmlPath));
-
- if (ds.Tables[0].Rows.Count > 0)
- {
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
-
- if (ds.Tables[0].Rows[i][strWhereColumnName].ToString().Trim().Equals(strWhereColumnValue))
- {
-
- for (int j = 0; j < Columns.Length; j++)
- {
- ds.Tables[0].Rows[i][Columns[j]] = ColumnValue[j];
- }
- ds.AcceptChanges();
- ds.WriteXml(GetXmlFullPath(strXmlPath));
- return true;
- }
- }
- }
- return false;
- }
- catch (Exception)
- {
- return false;
- }
- }
- #endregion
- #region 删除数据
-
-
-
-
-
-
-
-
-
-
- public static void Delete(string path, string node)
- {
- try
- {
- XmlDocument doc = XMLLoad(path);
- XmlNode xn = doc.SelectSingleNode(node);
- xn.ParentNode.RemoveChild(xn);
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- }
- catch { }
- }
-
-
-
-
-
-
-
-
-
-
- public static void Delete(string path, string node, string attribute)
- {
- try
- {
- XmlDocument doc = XMLLoad(path);
- XmlNode xn = doc.SelectSingleNode(node);
- XmlElement xe = (XmlElement)xn;
- if (attribute.Equals(""))
- xn.ParentNode.RemoveChild(xn);
- else
- xe.RemoveAttribute(attribute);
- doc.Save(AppDomain.CurrentDomain.BaseDirectory.ToString() + path);
- }
- catch { }
- }
-
-
-
-
- public static bool DeleteXmlAllRows(string strXmlPath)
- {
- try
- {
- DataSet ds = new DataSet();
- ds.ReadXml(GetXmlFullPath(strXmlPath));
- if (ds.Tables[0].Rows.Count > 0)
- {
- ds.Tables[0].Rows.Clear();
- }
- ds.WriteXml(GetXmlFullPath(strXmlPath));
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
-
-
-
- public static bool DeleteXmlRowByIndex(string strXmlPath, int iDeleteRow)
- {
- try
- {
- DataSet ds = new DataSet();
- ds.ReadXml(GetXmlFullPath(strXmlPath));
- if (ds.Tables[0].Rows.Count > 0)
- {
- ds.Tables[0].Rows[iDeleteRow].Delete();
- }
- ds.WriteXml(GetXmlFullPath(strXmlPath));
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
-
-
-
-
-
-
- public static bool DeleteXmlRows(string strXmlPath, string strColumn, string[] ColumnValue)
- {
- try
- {
- DataSet ds = new DataSet();
- ds.ReadXml(GetXmlFullPath(strXmlPath));
- if (ds.Tables[0].Rows.Count > 0)
- {
-
- if (ColumnValue.Length > ds.Tables[0].Rows.Count)
- {
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- for (int j = 0; j < ColumnValue.Length; j++)
- {
- if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
- {
- ds.Tables[0].Rows[i].Delete();
- }
- }
- }
- }
- else
- {
- for (int j = 0; j < ColumnValue.Length; j++)
- {
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- if (ds.Tables[0].Rows[i][strColumn].ToString().Trim().Equals(ColumnValue[j]))
- {
- ds.Tables[0].Rows[i].Delete();
- }
- }
- }
- }
- ds.WriteXml(GetXmlFullPath(strXmlPath));
- }
- return true;
- }
- catch (Exception)
- {
- return false;
- }
- }
- #endregion
- }
- }
|