/// <summary>
/// 将指定DataTable转换为其等效的Xml流形式。
/// </summary>
/// <param name= "table "> DataTable对象 </param>
/// <returns> </returns>
public string ToXmlString(DataTable table)
{
if(table == null)
{
return null;
}
StringBuilder content = new StringBuilder();
StringWriter writer = new StringWriter(content);
XmlTextWriter xmlWriter = new XmlTextWriter(writer);
try
{
DataSet ds = new DataSet();
ds.Tables.Add(table.Copy());
ds.WriteXml(xmlWriter);
return content.ToString();
}
finally
{
writer.Close();
xmlWriter.Close();
}
}
/// <summary>
/// 将指定的Xml流转换为其等效的DataSet对象。
/// </summary>
/// <param name= "xmlStream "> Xml流 </param>
/// <returns> </returns>
public DataSet ToDataSet(string xmlStream)
{
if(xmlStream == null || xmlStream.Trim().Length == 0)
{
return null;
}
StringReader reader = new StringReader(xmlStream);
XmlTextReader xmlReader = new XmlTextReader(reader);
try
{
DataSet ds = new DataSet();
ds.ReadXml(xmlReader);
return ds;
}
finally
{
reader.Close();
xmlReader.Close();
}
}