110,538
社区成员
发帖
与我相关
我的任务
分享
//将xml文件转换为DataSet
public static DataSet ConvertXMLFileToDataSet(string xmlFile)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlFile);
DataSet xmlDS = new DataSet();
stream = new StringReader(xmld.InnerXml);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
//xmlDS.ReadXml(xmlFile);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}
}
/// <summary>
/// 获取XML数据库中的数据的方法
/// </summary>
/// <param name="strFilePath">传入文件路径</param>
/// <returns>返回一个数据集</returns>
public static DataSet GetAllDataFromXML(string strFilePath)
{
DataSet ds = new DataSet();
FileInfo fileInfo = new FileInfo(strFilePath);
if (fileInfo.Exists)
{
try
{
ds.ReadXml(strFilePath);
}
catch { }
}
else
{
ds = null;
}
if (ds != null)
{
if (ds.Tables[0].Rows.Count < 1)
ds = null;
}
return ds;
}