一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net 将Xml文件设置为数据源实例

时间:2022-06-25 05:35:55 编辑:袖梨 来源:一聚教程网

asp教程.net 将Xml文件设置为数据源实例
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="gb2312" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.111com.net/1999/xhtml">





<%

asp.net教程 将Xml文件设置为数据源实例
///


        /// 读取Xml文件信息,并转换成DataSet对象
        ///

        ///
        /// DataSet ds = new DataSet();
        /// ds = CXmlFileToDataSet("/XML/upload.xml");
        ///

        /// Xml文件地址
        /// DataSet对象
        public static DataSet CXmlFileToDataSet(string xmlFilePath)
        {
            if (!string.IsNullOrEmpty(xmlFilePath))
            {
                //  string path =System.Windows.Forms.Application.StartupPath + System.Configuration.ConfigurationManager.AppSettings["flow"]+ xmlFilePath;

                string path = xmlFilePath;

                StringReader StrStream = null;

                XmlTextReader Xmlrdr = null;

                try
                {
                    XmlDocument xmldoc = new XmlDocument(); //根据地址加载Xml文件

                    xmldoc.Load(path);

                    DataSet ds = new DataSet(); //读取文件中的字符流

                    StrStream = new StringReader(xmldoc.InnerXml); //获取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;
            }
        }
%>

asp.net读取 xml实例

 

使用DataSet对象
<% @ Page Language="C#"%>
<% @ Import Namespace="System.Data"%>
<% @ Import Namespace="System.Data.OleDb"%>


 

读取XML方法三




热门栏目