XML操作类

using System;
using System.Xml;
using System.Data;

namespace XmlClass
    {
        /// <summary>
        /// XML相关通用功能
        /// </summary>
        public class XmlHelper
        {
            public XmlHelper() { }

            /// <summary>
            /// XML资源类型
            /// </summary>
            public enum XmlType
            {
                File,
                String
            };

            #region 读取XML资源到DataSet中
            /// <summary>
            /// 读取XML资源到DataSet中
            /// </summary>
            /// <param name="source">XML资源,文件为路径,否则为XML字符串</param>
            /// <param name="xmlType">XML资源类型</param>
            /// <returns>DataSet</returns>
            public static DataSet GetDataSet(string source, XmlType xmlType)
            {
                DataSet ds = new DataSet();
                if (xmlType == XmlType.File)
                {
                    ds.ReadXml(source);
                }
                else
                {
                    XmlDocument xd = new XmlDocument();
                    xd.LoadXml(source);
                    XmlNodeReader xnr = new XmlNodeReader(xd);
                    ds.ReadXml(xnr);
                }
                return ds;
            }

            #endregion

            #region 获取一个字符串xml文档中的ds
            /// <summary>
            /// 获取一个字符串xml文档中的ds
            /// </summary>
            /// <param name="xml_string">含有xml信息的字符串</param>
            public static void get_XmlValue_ds(string xml_string, ref DataSet ds)
            {
                System.Xml.XmlDocument xd = new XmlDocument();
                xd.LoadXml(xml_string);
                XmlNodeReader xnr = new XmlNodeReader(xd);
                ds.ReadXml(xnr);
                xnr.Close();
                int a = ds.Tables.Count;
            }
            #endregion

            #region 读取XML资源到DataTable中
            /// <summary>
            /// 读取XML资源到DataTable中
            /// </summary>
            /// <param name="source">XML资源,文件为路径,否则为XML字符串</param>
            /// <param name="xmlType">XML资源类型:文件,字符串</param>
            /// <param name="tableName">表名称</param>
            /// <returns>DataTable</returns>
            public static DataTable GetTable(string source, XmlType xmlType, string tableName)
            {
                DataSet ds = new DataSet();
                if (xmlType == XmlType.File)
                {
                    ds.ReadXml(source);
                }
                else
                {
                    XmlDocument xd = new XmlDocument();
                    xd.LoadXml(source);
                    XmlNodeReader xnr = new XmlNodeReader(xd);
                    ds.ReadXml(xnr);
                }
                return ds.Tables[tableName];
            }
            #endregion

            #region 读取XML资源中指定的DataTable的指定行指定列的值
            /// <summary>
            /// 读取XML资源中指定的DataTable的指定行指定列的值
            /// </summary>
            /// <param name="source">XML资源</param>
            /// <param name="xmlType">XML资源类型:文件,字符串</param>
            /// <param name="tableName">表名</param>
            /// <param name="rowIndex">行号</param>
            /// <param name="colName">列名</param>
            /// <returns>值,不存在时返回Null</returns>
            public static object GetTableCell(string source, XmlType xmlType, string tableName, int rowIndex, string colName)
            {
                DataSet ds = new DataSet();
                if (xmlType == XmlType.File)
                {
                    ds.ReadXml(source);
                }
                else
                {
                    XmlDocument xd = new XmlDocument();
                    xd.LoadXml(source);
                    XmlNodeReader xnr = new XmlNodeReader(xd);
                    ds.ReadXml(xnr);
                }
                return ds.Tables[tableName].Rows[rowIndex][colName];
            }
            #endregion

            #region 读取XML资源中指定的DataTable的指定行指定列的值
            /// <summary>
            /// 读取XML资源中指定的DataTable的指定行指定列的值
            /// </summary>
            /// <param name="source">XML资源</param>
            /// <param name="xmlType">XML资源类型:文件,字符串</param>
            /// <param name="tableName">表名</param>
            /// <param name="rowIndex">行号</param>
            /// <param name="colIndex">列号</param>
            /// <returns>值,不存在时返回Null</returns>
            public static object GetTableCell(string source, XmlType xmlType, string tableName, int rowIndex, int colIndex)
            {
                DataSet ds = new DataSet();
                if (xmlType == XmlType.File)
                {
                    ds.ReadXml(source);
                }
                else
                {
                    XmlDocument xd = new XmlDocument();
                    xd.LoadXml(source);
                    XmlNodeReader xnr = new XmlNodeReader(xd);
                    ds.ReadXml(xnr);
                }
                return ds.Tables[tableName].Rows[rowIndex][colIndex];
            }
            #endregion

            #region 将DataTable写入XML文件中
            /// <summary>
            /// 将DataTable写入XML文件中
            /// </summary>
            /// <param name="dt">含有数据的DataTable</param>
            /// <param name="filePath">文件路径</param>
            public static void SaveTableToFile(DataTable dt, string filePath)
            {
                DataSet ds = new DataSet("Config");
                ds.Tables.Add(dt.Copy());
                ds.WriteXml(filePath);
            }
            #endregion

            #region 将DataTable以指定的根结点名称写入文件
            /// <summary>
            /// 将DataTable以指定的根结点名称写入文件
            /// </summary>
            /// <param name="dt">含有数据的DataTable</param>
            /// <param name="rootName">根结点名称</param>
            /// <param name="filePath">文件路径</param>
            public static void SaveTableToFile(DataTable dt, string rootName, string filePath)
            {
                DataSet ds = new DataSet(rootName);
                ds.Tables.Add(dt.Copy());
                ds.WriteXml(filePath);
            }
            #endregion

            #region 使用DataSet方式更新XML文件节点
            /// <summary>
            /// 使用DataSet方式更新XML文件节点
            /// </summary>
            /// <param name="filePath">XML文件路径</param>
            /// <param name="tableName">表名称</param>
            /// <param name="rowIndex">行号</param>
            /// <param name="colName">列名</param>
            /// <param name="content">更新值</param>
            /// <returns>更新是否成功</returns>
            public static bool UpdateTableCell(string filePath, string tableName, int rowIndex, string colName, string content)
            {
                bool flag = false;
                DataSet ds = new DataSet();
                ds.ReadXml(filePath);
                DataTable dt = ds.Tables[tableName];

                if (dt.Rows[rowIndex][colName] != null)
                {
                    dt.Rows[rowIndex][colName] = content;
                    ds.WriteXml(filePath);
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                return flag;
            }
            #endregion

            #region 使用DataSet方式更新XML文件节点
            /// <summary>
            /// 使用DataSet方式更新XML文件节点
            /// </summary>
            /// <param name="filePath">XML文件路径</param>
            /// <param name="tableName">表名称</param>
            /// <param name="rowIndex">行号</param>
            /// <param name="colIndex">列号</param>
            /// <param name="content">更新值</param>
            /// <returns>更新是否成功</returns>
            public static bool UpdateTableCell(string filePath, string tableName, int rowIndex, int colIndex, string content)
            {
                bool flag = false;

                DataSet ds = new DataSet();
                ds.ReadXml(filePath);
                DataTable dt = ds.Tables[tableName];

                if (dt.Rows[rowIndex][colIndex] != null)
                {
                    dt.Rows[rowIndex][colIndex] = content;
                    ds.WriteXml(filePath);
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                return flag;
            }
            #endregion

            #region 读取XML资源中的指定节点内容
            /// <summary>
            /// 读取XML资源中的指定节点内容
            /// </summary>
            /// <param name="source">XML资源</param>
            /// <param name="xmlType">XML资源类型:文件,字符串</param>
            /// <param name="nodeName">节点名称</param>
            /// <returns>节点内容</returns>
            public static object GetNodeValue(string source, XmlType xmlType, string nodeName)
            {
                XmlDocument xd = new XmlDocument();
                if (xmlType == XmlType.File)
                {
                    xd.Load(source);
                }
                else
                {
                    xd.LoadXml(source);
                }
                XmlElement xe = xd.DocumentElement;
                XmlNode xn = xe.SelectSingleNode("//" + nodeName);
                if (xn != null)
                {
                    return xn.InnerText;
                }
                else
                {
                    return null;
                }
            }

            /// <summary>
            /// 读取XML资源中的指定节点内容
            /// </summary>
            /// <param name="source">XML资源</param>
            /// <param name="nodeName">节点名称</param>
            /// <returns>节点内容</returns>
            public static object GetNodeValue(string source, string nodeName)
            {
                if (source == null || nodeName == null || source == "" || nodeName == "" || source.Length < nodeName.Length * 2)
                {
                    return null;
                }
                else
                {
                    int start = source.IndexOf("<" + nodeName + ">") + nodeName.Length + 2;
                    int end = source.IndexOf("</" + nodeName + ">");
                    if (start == -1 || end == -1)
                    {
                        return null;
                    }
                    else if (start >= end)
                    {
                        return null;
                    }
                    else
                    {
                        return source.Substring(start, end - start);
                    }
                }
            }
            #endregion

            #region 更新XML文件中的指定节点内容
            /// <summary>
            /// 更新XML文件中的指定节点内容
            /// </summary>
            /// <param name="filePath">文件路径</param>
            /// <param name="nodeName">节点名称</param>
            /// <param name="nodeValue">更新内容</param>
            /// <returns>更新是否成功</returns>
            public static bool UpdateNode(string filePath, string nodeName, string nodeValue)
            {
                bool flag = false;

                XmlDocument xd = new XmlDocument();
                xd.Load(filePath);
                XmlElement xe = xd.DocumentElement;
                XmlNode xn = xe.SelectSingleNode("//" + nodeName);
                if (xn != null)
                {
                    xn.InnerText = nodeValue;
                    flag = true;
                }
                else
                {
                    flag = false;
                }
                return flag;
            }
            #endregion

            #region 操作xml文件中指定节点的数据
            /// <summary>
            /// 获得xml文件中指定节点的节点数据
            /// </summary>
            /// <param name="TableName"></param>
            /// <returns></returns>
            public static string GetNodeInfoByNodeName(string path, string nodeName)
            {
                string XmlString = "";
                XmlDocument xml = new XmlDocument();
                xml.Load(path);
                System.Xml.XmlElement root = xml.DocumentElement;
                System.Xml.XmlNode node = root.SelectSingleNode("//" + nodeName);
                if (node != null)
                {
                    XmlString = node.InnerText;
                }
                return XmlString;
            }
            #endregion
        }
    }

 

posted @ 2012-05-31 18:35  爱智旮旯  阅读(1236)  评论(1编辑  收藏  举报