首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-02-09 
求问XML读取到DataSet中的问题.N久不写代码已生疏。感谢一个XML,含多个表,及关系。想将其读入一个DataSet中,

求问XML读取到DataSet中的问题.N久不写代码已生疏。感谢
一个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();            }        } 

热点排行