首页 新闻 赞助 找找看

xml读取:第一次读取的时候可以,第二次读取就报错了:未将对象引用到实例

0
[已解决问题] 解决于 2018-02-02 11:18
        private string GetRequestUrl(string ServerTag)
        {
            DataTable dt = new DataTable();
            string url = string.Empty;
            //XmlDocument doc = new XmlDocument();
            if (!Directory.Exists(filePath))
                Directory.CreateDirectory(filePath);
            if (!File.Exists(fullPath))
                File.Create(fullPath);
            if (!string.IsNullOrEmpty(ServerTag))
            {
                if (dt.Rows.Count <= 0)
                {
                    XmlOp op = new XmlOp(fullPath);//装载xml文件(第二次读取报错
                    dt = op.GetDs("Root").Tables[0];                    
                }
                DataRow[] dataRow = dt.Select(string.Format("Name='{0}'", ServerTag));
                if (dataRow != null && dataRow.Count() > 0)
                {
                    return dataRow[0][1].ToString();//筛选表中请求的地址
                }
                else
                    return "https://www.baidu.com/";
            }
            return "";
        }

报错得地方:

复制代码
        /// <summary>
        /// 构造函数,导入Xml文件
        /// </summary>
        /// <param name="xmlFile">文件虚拟路径</param>
        public XmlOp(string xmlFile)
        {
            try
            {
                XmlDoc.Load(xmlFile);   //第二次载入Xml文档的时候报错(错误提示:未将对象引用实例
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
复制代码

大da脸的主页 大da脸 | 初学一级 | 园豆:73
提问于:2018-02-02 10:58
< >
分享
最佳答案
0

你第二次运行,设个断点在

XmlDoc.Load这行上面, 不要运行,先看看XmlDoc 是否为null。

奖励园豆:5
爱编程的大叔 | 高人七级 |园豆:30839 | 2018-02-02 11:03

大da脸 | 园豆:73 (初学一级) | 2018-02-02 11:04

XmlDoc确实是NULL

大da脸 | 园豆:73 (初学一级) | 2018-02-02 11:08

第一次读取的时候XmlDoc=document

第二次读取的时候XmlDoc=NULL

那这要怎么处理?

大da脸 | 园豆:73 (初学一级) | 2018-02-02 11:12

已解决:上面声明的 XmlDocument static,去除static就可以了

大da脸 | 园豆:73 (初学一级) | 2018-02-02 11:18
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册