/// <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, XmlTypeEnums xmlType, string tableName) { DataSet ds = new DataSet(); if (xmlType == XmlTypeEnums.File) { ds.ReadXml(source); } else { XmlDocument xd = new XmlDocument(); xd.LoadXml(source); XmlNodeReader xnr = new XmlNodeReader(xd); ds.ReadXml(xnr); } return(ds.Tables[tableName]); }
/// <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, XmlTypeEnums xmlType, string tableName, int rowIndex, int colIndex) { DataSet ds = new DataSet(); if (xmlType == XmlTypeEnums.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]); }
/// <summary> /// 读取XML资源到DataSet中 /// </summary> /// <param name="source">XML资源,文件为路径,否则为XML字符串</param> /// <param name="xmlType">XML资源类型</param> /// <returns>DataSet</returns> public static DataSet GetDataSet(string source, XmlTypeEnums xmlType) { DataSet ds = new DataSet(); if (xmlType == XmlTypeEnums.File) { ds.ReadXml(source); } else { XmlDocument xd = new XmlDocument(); xd.LoadXml(source); XmlNodeReader xnr = new XmlNodeReader(xd); ds.ReadXml(xnr); } return(ds); }
/// <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, XmlTypeEnums xmlType, string nodeName) { XmlDocument xd = new XmlDocument(); if (xmlType == XmlTypeEnums.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); } }