/**//// <summary>
/// XML形式的字符串、XML文江转换成DataSet、DataTable格式
/// </summary>
public class XmlToData
{
/**//// <summary>
/// 将Xml内容字符串转换成DataSet对象
/// </summary>
/// <param name="xmlStr">Xml内容字符串</param>
/// <returns>DataSet对象</returns>
public static DataSet CXmlToDataSet(string xmlStr)
{
if (!string.IsNullOrEmpty(xmlStr))
{
StringReader StrStream = null;
XmlTextReader Xmlrdr = null;
try
{
DataSet ds = new DataSet();
//读取字符串中的信息
StrStream = new StringReader(xmlStr);
//获取StrStream中的数据
Xmlrdr = new XmlTextReader(StrStream);
//ds获取Xmlrdr中的数据
ds.ReadXml(Xmlrdr);
return ds;
}
catch (Exception e)
{
throw e;
}
finally
{
//释放资源
if (Xmlrdr != null)
{
Xmlrdr.Close();
StrStream.Close();
StrStream.Dispose();
}
}
}
else
{
return null;
}
}
或者
#region 将XML转换成DataSet
/// <summary>
/// 将XML转换成DataSet
/// </summary>
/// <param name="xmlData"></param>
/// <returns></returns>
private
static
DataSet ConvertXMLToDataSet(
string
xmlData)
{
StringReader stream =
null
;
XmlTextReader reader =
null
;
try
{
DataSet xmlDS =
new
DataSet();
stream =
new
StringReader(xmlData);
reader =
new
XmlTextReader(stream);
xmlDS.ReadXml(reader);
return
xmlDS;
}
catch
(Exception ex)
{
string
strTest = ex.Message;
return
null
;
}
finally
{
if
(reader !=
null
)
{
reader.Close();
}
}
}
#endregion
XmlTextReader reader = null;
DataTable dt = new DataTable ();
stream = new StringReader(xmlStringData);
reader = new XmlTextReader(stream);
dt.ReadXml(reader);
reader.Close();