求问XML读取到DataSet中的有关问题.N久不写代码已生疏。感谢
求问XML读取到DataSet中的问题.N久不写代码已生疏。感谢
一个XML,含多个表,及关系。
想将其读入一个DataSet中,提笔忘字,忘高手相助。感谢!
DataSet预先不知道XSD架构。全凭读取XML去解析。
------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/ekw4dh3f(v=VS.100).aspx
------解决方案--------------------
一个XML,含多个表,及关系。
想将其读入一个DataSet中,提笔忘字,忘高手相助。感谢!
DataSet预先不知道XSD架构。全凭读取XML去解析。
------解决方案--------------------
http://msdn.microsoft.com/zh-cn/library/ekw4dh3f(v=VS.100).aspx
------解决方案--------------------
- C# code
/// <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; }
------解决方案--------------------
- C# code
//将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(); } }