You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
476 lines
14 KiB
476 lines
14 KiB
using System.Data;
|
|
using System;
|
|
using System.Xml;
|
|
using System.IO;
|
|
using System.Xml.Serialization;
|
|
namespace SiaSun.LMS.Common
|
|
{
|
|
/// <summary>
|
|
/// OperateXmlByDataSet 的摘要说明。
|
|
/// </summary>
|
|
public class Xml
|
|
{
|
|
private string strXmlPath;//这个是相对路径
|
|
public Xml(string strXmlPath)
|
|
{
|
|
//
|
|
// TODO: 在此处添加构造函数逻辑
|
|
//
|
|
this.strXmlPath = strXmlPath;
|
|
}
|
|
|
|
public Xml()
|
|
{
|
|
}
|
|
|
|
public DataTable GetDataTable_0(string sXml)
|
|
{
|
|
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None);
|
|
|
|
XmlTextReader reader = new XmlTextReader(sXml, XmlNodeType.Element, context);
|
|
|
|
DataSet ds = new DataSet();
|
|
|
|
ds.ReadXml(reader);
|
|
|
|
return (ds.Tables.Count > 0) ? ds.Tables[0] : null;
|
|
}
|
|
|
|
public DataTable GetDataTable(string sXml)
|
|
{
|
|
XmlParserContext context = new XmlParserContext(null, null, null, XmlSpace.None);
|
|
|
|
XmlTextReader reader = new XmlTextReader(sXml, XmlNodeType.Document, context);
|
|
|
|
DataSet ds = new DataSet();
|
|
|
|
ds.ReadXml(reader);
|
|
|
|
return (ds.Tables.Count > 1) ? ds.Tables[1] : null;
|
|
}
|
|
|
|
#region GetDataSetByXml
|
|
/// <summary>
|
|
/// 读取xml直接返回DataSet
|
|
/// </summary>
|
|
/// <param name="strXmlPath">xml文件相对路径</param>
|
|
/// <returns></returns>
|
|
public DataSet GetDataSetByXml()
|
|
{
|
|
try
|
|
{
|
|
DataSet ds = new DataSet();
|
|
|
|
ds.ReadXml(GetXmlFullPath(strXmlPath));
|
|
if (ds.Tables.Count > 0)
|
|
{
|
|
return ds;
|
|
}
|
|
return null;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/*
|
|
* 以下代码读取到一个没有排序和筛选的DataSet。
|
|
|
|
USTC.XmlDataSet XML=new XmlDataSet(@"XML/PortalCfg.xml");
|
|
DataGrid1.DataSource = XML.GetDataSetByXml();
|
|
DataGrid1.DataBind();
|
|
//以下代码读到的数据是经过筛选和排序的:
|
|
DataGrid1.DataSource =XML.GetDataViewByXml(
|
|
"name = 'Asp.net'", //条件:name列值为Asp.net
|
|
"peopleNum desc"); //按peopleNum列降序排列
|
|
DataGrid1.DataBind();
|
|
|
|
*/
|
|
|
|
#region GetDataViewByXml
|
|
/// <summary>
|
|
/// 读取Xml返回一个经排序或筛选后的DataView
|
|
/// </summary>
|
|
/// <param name="strXmlPath"></param>
|
|
/// <param name="strWhere">筛选条件,如:"name = 'kgdiwss'"</param>
|
|
/// <param name="strSort">排序条件,如:"Id desc"</param>
|
|
/// <returns></returns>
|
|
public DataView GetDataViewByXml(string strWhere, string strSort)
|
|
{
|
|
try
|
|
{
|
|
DataSet ds = new DataSet();
|
|
ds.ReadXml(GetXmlFullPath(strXmlPath));
|
|
DataView dv = new DataView(ds.Tables[0]);//只显示第一个表
|
|
if (strSort.Trim() != "")
|
|
{
|
|
dv.Sort = strSort;
|
|
}
|
|
if (strWhere.Trim() != "")
|
|
{
|
|
dv.RowFilter = strWhere;
|
|
}
|
|
return dv;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/* 以下代码向XML文件中添加了一条记录,同时给7个列赋值:
|
|
|
|
|
|
bool b;
|
|
USTC.XmlDataSet XML=new XmlDataSet(@"XML/PortalCfg.xml");
|
|
b = XML.WriteXmlByDataSet(
|
|
new string[]{
|
|
"name", //姓名字段
|
|
"peopleNum", //人数字段
|
|
"address", //地址字段
|
|
"description", //描述字段
|
|
"require", //需求字段
|
|
"deadLine", //结束时间字段
|
|
"IsMarried" //婚否字段
|
|
},
|
|
new string[]{
|
|
"Asp.net程序员", //姓名字段值
|
|
"2", //人数字段值
|
|
"建国路", //地址字段值
|
|
"B/S结构程序", //描述字段值
|
|
"asp.net c#等", //需求字段值
|
|
DateTime.Now.ToShortDateString(), //结束时间字段值
|
|
"false" //婚否字段值
|
|
});
|
|
|
|
如果b返回值为true,表示添加成功,否则表示添加失败。以上的写法我用了些偷懒的方法,比如我把数组直接放在参数,而没有另外申明,事实上你可以另外申明一个数组,然后再传到方法中。
|
|
请注意字段在数组中的位置和值在数组中的位置的对应关系。
|
|
*/
|
|
#region WriteXmlByDataSet
|
|
/// <summary>
|
|
/// 向Xml文件插入一行数据
|
|
/// </summary>
|
|
/// <param name="strXmlPath">xml文件相对路径</param>
|
|
/// <param name="Columns">要插入行的列名数组,如:string[] Columns = {"name","IsMarried"};</param>
|
|
/// <param name="ColumnValue">要插入行每列的值数组,如:string[] ColumnValue={"明天去要饭","false"};</param>
|
|
/// <returns>成功返回true,否则返回false</returns>
|
|
public string WriteXmlByDataSet(string[] Columns, string[] ColumnValue)
|
|
{
|
|
try
|
|
{
|
|
//根据传入的XML路径得到.XSD的路径,两个文件放在同一个目录下
|
|
string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd";
|
|
|
|
DataSet ds = new DataSet();
|
|
//读xml架构,关系到列的数据类型
|
|
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 ex)
|
|
{
|
|
string t = ex.ToString();
|
|
return t;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
|
|
/*
|
|
* 以下代码将找到peopleNum列值为3的行,然后将行的name、peopleNum、、description和IsMarried四个字段的值分别更新成kgdiwss、10、描述、true。
|
|
|
|
|
|
bool b;
|
|
USTC.XmlDataSet XML=new XmlDataSet(@"XML/PortalCfg.xml");
|
|
b =XML.UpdateXmlRow(
|
|
new string[]{"name","peopleNum","description","IsMarried"},
|
|
new string[]{"kgdiwss","10","描述","true"},
|
|
"peopleNum",
|
|
"3");
|
|
|
|
|
|
返回true表示修改成功,否则表示修改失败。
|
|
请特别注意,字段类型为逻辑型时,赋值用的是true和false,而不是0和1。
|
|
|
|
|
|
* */
|
|
#region UpdateXmlRow
|
|
/// <summary>
|
|
/// 更行符合条件的一条Xml记录
|
|
/// </summary>
|
|
/// <param name="strXmlPath">XML文件路径</param>
|
|
/// <param name="Columns">列名数组</param>
|
|
/// <param name="ColumnValue">列值数组</param>
|
|
/// <param name="strWhereColumnName">条件列名</param>
|
|
/// <param name="strWhereColumnValue">条件列值</param>
|
|
/// <returns></returns>
|
|
public bool UpdateXmlRow(string[] Columns, string[] ColumnValue, string strWhereColumnName, string strWhereColumnValue)
|
|
{
|
|
try
|
|
{
|
|
string strXsdPath = strXmlPath.Substring(0, strXmlPath.IndexOf(".")) + ".xsd";//假定XSD就在同目录下
|
|
|
|
DataSet ds = new DataSet();
|
|
//读xml架构,关系到列的数据类型
|
|
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++)
|
|
{
|
|
//如果当前记录为符合Where条件的记录
|
|
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];
|
|
}
|
|
//更新DataSet
|
|
ds.AcceptChanges();
|
|
//重新写入XML文件
|
|
ds.WriteXml(GetXmlFullPath(strXmlPath));
|
|
return true;
|
|
}
|
|
}
|
|
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
/*
|
|
* 以下代码实现删除name列值为数组中的值的行。
|
|
bool b;
|
|
USTC.XmlDataSet XML=new XmlDataSet(@"XML/PortalCfg.xml");
|
|
b = XML.DeleteXmlRows(
|
|
"name", //条件列
|
|
new string[]{
|
|
"值1", //条件值1
|
|
"值2", //条件值2
|
|
"值3" //条件值3
|
|
});
|
|
|
|
上面代码执行成功后,name列值为值1、值2、值3的行将被删除。
|
|
删除成功返回true,否则返回false。
|
|
|
|
|
|
*/
|
|
#region DeleteXmlRowByIndex
|
|
/// <summary>
|
|
/// 通过删除DataSet中iDeleteRow这一行,然后重写Xml以实现删除指定行
|
|
/// </summary>
|
|
/// <param name="strXmlPath"></param>
|
|
/// <param name="iDeleteRow">要删除的行在DataSet中的Index值</param>
|
|
public bool DeleteXmlRowByIndex(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;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DeleteXmlRows
|
|
/// <summary>
|
|
/// 删除strColumn列中值为ColumnValue的行
|
|
/// </summary>
|
|
/// <param name="strXmlPath">xml相对路径</param>
|
|
/// <param name="strColumn">列名</param>
|
|
/// <param name="ColumnValue">strColumn列中值为ColumnValue的行均会被删除</param>
|
|
/// <returns></returns>
|
|
public string DeleteXmlRows(string strColumn, string[] ColumnValue)
|
|
{
|
|
try
|
|
{
|
|
DataSet ds = new DataSet();
|
|
|
|
ds.ReadXml(GetXmlFullPath(strXmlPath));
|
|
|
|
//先判断行数
|
|
if (ds.Tables[0].Rows.Count > 0)
|
|
{
|
|
//判断行多还是删除的值多,多的for循环放在里面
|
|
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 ex)
|
|
{
|
|
string t = ex.ToString();
|
|
return t;
|
|
}
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region DeleteXmlAllRows
|
|
/// <summary>
|
|
/// 删除所有行
|
|
/// </summary>
|
|
/// <param name="strXmlPath">XML路径</param>
|
|
/// <returns></returns>
|
|
public bool DeleteXmlAllRows()
|
|
{
|
|
try
|
|
{
|
|
DataSet ds = new DataSet();
|
|
ds.ReadXml(GetXmlFullPath(strXmlPath));
|
|
//如果记录条数大于0
|
|
if (ds.Tables[0].Rows.Count > 0)
|
|
{
|
|
//移除所有记录
|
|
ds.Tables[0].Rows.Clear();
|
|
}
|
|
//重新写入,这时XML文件中就只剩根节点了
|
|
ds.WriteXml(GetXmlFullPath(strXmlPath));
|
|
return true;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region GetXmlFullPath
|
|
/// <summary>
|
|
/// 返回完整路径
|
|
/// </summary>
|
|
/// <param name="strPath">Xml的路径</param>
|
|
/// <returns></returns>
|
|
public string GetXmlFullPath(string strPath)
|
|
{
|
|
if (strPath.IndexOf(":") > 0)
|
|
{
|
|
return strPath;
|
|
}
|
|
else
|
|
{
|
|
return System.Web.HttpContext.Current.Server.MapPath(strPath);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 方法
|
|
|
|
/// <summary> 反序列化
|
|
/// 反序列化
|
|
/// </summary>
|
|
/// <param name="type">类型</param>
|
|
/// <param name="xml">XML字符串</param>
|
|
/// <returns></returns>
|
|
public static T DeSerializer<T>(string xml)
|
|
{
|
|
try
|
|
{
|
|
using (StringReader sr = new StringReader(xml))
|
|
{
|
|
XmlSerializer xmldes = new XmlSerializer(typeof(T));
|
|
|
|
return (T)xmldes.Deserialize(sr);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return default(T);
|
|
}
|
|
}
|
|
|
|
/// <summary> 序列化XML文件
|
|
/// 序列化XML文件
|
|
/// </summary>
|
|
/// <param name="type">类型</param>
|
|
/// <param name="obj">对象</param>
|
|
/// <returns></returns>
|
|
public static string Serializer<T>(T t)
|
|
{
|
|
MemoryStream Stream = new MemoryStream();
|
|
|
|
//创建序列化对象
|
|
XmlSerializer xml = new XmlSerializer(typeof(T));
|
|
|
|
try
|
|
{
|
|
//序列化对象
|
|
xml.Serialize(Stream, t);
|
|
}
|
|
catch (InvalidOperationException)
|
|
{
|
|
throw;
|
|
}
|
|
|
|
Stream.Position = 0;
|
|
|
|
StreamReader sr = new StreamReader(Stream);
|
|
|
|
string str = sr.ReadToEnd().Replace(" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", string.Empty);
|
|
|
|
return str;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|