using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Xml;
namespace IwbZero.ToolCommon.FileHelpers
{
///
/// 把DataSet、DataTable、DataView格式转换成XML字符串、XML文件
///
public static class DataToXml
{
/////
///// 将DataTable对象转换成XML字符串
/////
///// DataTable对象
///// XML字符串
//public static string CDataToXml(DataTable dt)
//{
// if (dt != null)
// {
// MemoryStream ms = null;
// XmlTextWriter XmlWt = null;
// try
// {
// ms = new MemoryStream();
// //根据ms实例化XmlWt
// XmlWt = new XmlTextWriter(ms, Encoding.Unicode);
// //获取ds中的数据
// dt.WriteXml(XmlWt);
// int count = (int)ms.Length;
// byte[] temp = new byte[count];
// ms.Seek(0, SeekOrigin.Begin);
// ms.Read(temp, 0, count);
// //返回Unicode编码的文本
// UnicodeEncoding ucode = new UnicodeEncoding();
// string returnValue = ucode.GetString(temp).Trim();
// return returnValue;
// }
// catch (System.Exception ex)
// {
// throw ex;
// }
// finally
// {
// //释放资源
// if (XmlWt != null)
// {
// XmlWt.Close();
// ms.Close();
// ms.Dispose();
// }
// }
// }
// else
// {
// return "";
// }
//}
//以上转换在一些xml版本中存在问题,会出现SoapToolkit调用的时候返回值错误,那么这时候可以用以下这个方法做转换(建议大家都用以下这个方法.......因为本人曾经深受上面那个方法其害......T_T)。
///
/// 将DataTable对象转换成XML字符串
///
/// DataTable对象
/// XML字符串
public static string CDataToXml(this DataTable dt)
{
StringBuilder strXml = new StringBuilder();
strXml.AppendLine("");
for (int i = 0; i < dt.Rows.Count; i++)
{
strXml.AppendLine("");
for (int j = 0; j < dt.Columns.Count; j++)
{
strXml.AppendLine("<" + dt.Columns[j].ColumnName + ">" + dt.Rows[i][j] + "" + dt.Columns[j].ColumnName + ">");
}
strXml.AppendLine("");
}
strXml.AppendLine("");
return strXml.ToString();
}
///
/// 将DataSet对象中指定的Table转换成XML字符串
///
/// DataSet对象
/// DataSet对象中的Table索引
/// XML字符串
public static string CDataToXml(this DataSet ds, int tableIndex)
{
if (tableIndex != -1)
{
return CDataToXml(ds.Tables[tableIndex]);
}
else
{
return CDataToXml(ds.Tables[0]);
}
}
/**////
/// 将DataSet对象转换成XML字符串
///
/// DataSet对象
/// XML字符串
public static string CDataToXml(DataSet ds)
{
return CDataToXml(ds, -1);
}
/**////
/// 将DataView对象转换成XML字符串
///
/// DataView对象
/// XML字符串
public static string CDataToXml(DataView dv)
{
return CDataToXml(dv.Table);
}
/**////
/// 将DataSet对象数据保存为XML文件
///
/// DataSet
/// XML文件路径
/// bool值
public static bool CDataToXmlFile(this DataTable dt, string xmlFilePath)
{
if ((dt != null) && (!string.IsNullOrEmpty(xmlFilePath)))
{
var xDoc = new XmlDocument {InnerXml = CDataToXml(dt)};
xDoc.Save(xmlFilePath);
return true;
}
return false;
}
/**////
/// 将DataSet对象中指定的Table转换成XML文件
///
/// DataSet对象
/// DataSet对象中的Table索引
/// xml文件路径
/// bool]值
public static bool CDataToXmlFile(DataSet ds, int tableIndex, string xmlFilePath)
{
if (tableIndex != -1)
{
return CDataToXmlFile(ds.Tables[tableIndex], xmlFilePath);
}
return CDataToXmlFile(ds.Tables[0], xmlFilePath);
}
/**////
/// 将DataSet对象转换成XML文件
///
/// DataSet对象
/// xml文件路径
/// bool]值
public static bool CDataToXmlFile(DataSet ds, string xmlFilePath)
{
return CDataToXmlFile(ds, -1, xmlFilePath);
}
/**////
/// 将DataView对象转换成XML文件
///
/// DataView对象
/// xml文件路径
/// bool]值
public static bool CDataToXmlFile(DataView dv, string xmlFilePath)
{
return CDataToXmlFile(dv.Table, xmlFilePath);
}
}
}