求问XML读取到DataSet中的问题.N久不写代码已生疏。感谢

wdgphc 2012-02-01 09:36:52
一个XML,含多个表,及关系。
想将其读入一个DataSet中,提笔忘字,忘高手相助。感谢!
DataSet预先不知道XSD架构。全凭读取XML去解析。
...全文
64 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ycproc 2012-02-01
  • 打赏
  • 举报
回复
 //将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();
}
}
熙风 2012-02-01
  • 打赏
  • 举报
回复
    /// <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;
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧