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
11 months ago
|
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��ʾ�ijɹ���������ʾ��ʧ�ܡ�
|
||
|
�������ر�ע�⣬�ֶ�����Ϊ����ʱ����ֵ�õ���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";//�ٶ��أӣľ���ͬĿ¼��
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|