此文档已具有 'XmlDeclaration' 节点

paleyyang 2011-04-07 03:44:34
读取XML配置文件.
麻烦各位高手指点一二..多谢!

这是XML文件读取类.
public class Cls_ConfigReader : System.Web.UI.Page
{
private static XmlDocument xmldoc;

public static void ConfigReader(string xmlFile)
{
xmldoc = new XmlDocument();
xmldoc.Load(HttpContext.Current.Server.MapPath(@"~/" + xmlFile));
}
public static List<Cls_Goods> wb_getGoodsInfo(string _ucode)
{
List<Cls_Goods> mylist = null;
try
{
XmlNode xnode = xmldoc.SelectSingleNode("userconfig/user[@code='" + _ucode.ToUpper() + "']");
if (xnode != null)
{
mylist = new List<Cls_Goods>();
XmlNodeList ndlist = xnode.SelectNodes("goodsconfig/goods");
foreach (XmlNode nd in ndlist)
{
Cls_Goods goods = new Cls_Goods();
goods.TradeCodeOnLine = nd.Attributes["code"].Value;
goods.ExchangeCode = nd.Attributes["excode"].Value;
goods.TradeName = nd.InnerText;
mylist.Add(goods);
}
}
}
catch
{
throw new Exception("请检查配置文件!");
}
return mylist;
}
public static List<Cls_Title> wb_getTitle(string _ucode)
{
List<Cls_Title> titles = null;
try
{
//得到当前用户的配置节点
XmlNode xnode = xmldoc.SelectSingleNode("userconfig/user[@code='" + _ucode.ToUpper() + "']");
if (xnode != null)
{
titles = new List<Cls_Title>();
XmlNodeList ndlist = xnode.SelectNodes("titles/title");
foreach (XmlNode nd in ndlist)
{
Cls_Title title = new Cls_Title();
title.titlename = nd.InnerText;
title.titlecode = nd.Attributes["code"].Value;
titles.Add(title);
}
}
}
catch
{

throw new Exception("请检查配置文件!");
}

return titles;
}
}

下面是其中两个调用XML读取类的方法

public static Cls_UserInfo GetUserInfo(string userid,string xmlFile)
{
string thekey = userid + "---Cls_UserInfo---" + CODE_CACHE_KEY + "---GetUserInfo---" + xmlFile;
Cls_UserInfo user = null;
rwlock.AcquireReaderLock(Timeout.Infinite);
try
{
if (HttpContext.Current.Cache[thekey] != null)
{
user = (Cls_UserInfo)HttpContext.Current.Cache[thekey];
}
else
{
LockCookie lc = rwlock.UpgradeToWriterLock(Timeout.Infinite);
try
{
for (int i = 0; i < 5; i++)
{
if ( user == null)
{
Cls_ConfigReader.ConfigReader(xmlFile);
user = Cls_ConfigReader.wb_getUserInfo(userid);
}
}
if (user != null)
{
HttpContext.Current.Cache.Insert(thekey, user, null, DateTime.Now.AddSeconds(CACHE_Min), System.Web.Caching.Cache.NoSlidingExpiration);
}

}
finally
{
rwlock.DowngradeFromWriterLock(ref lc);
}
}
return user;
}
finally
{
rwlock.ReleaseReaderLock();
}
}


public static List<Cls_Title> GetUserTitleInfo(string userid, string xmlFile)
{
string thekey = userid + "List<Cls_Title>" + CODE_CACHE_KEY + "GetUserTitleInfo" + xmlFile;
List<Cls_Title> titlelist = null;
rwlock.AcquireReaderLock(Timeout.Infinite);
try
{
if (HttpContext.Current.Cache[thekey] != null)
{
titlelist = (List<Cls_Title>)HttpContext.Current.Cache[thekey];
}
else
{
LockCookie lc = rwlock.UpgradeToWriterLock(Timeout.Infinite);
try
{
for (int i = 0; i < 5; i++)
{
if (titlelist == null )
{
Cls_ConfigReader.ConfigReader(xmlFile);
titlelist = Cls_ConfigReader.wb_getTitle(userid);
}
}
if (titlelist != null)
{
HttpContext.Current.Cache.Insert(thekey, titlelist, null, DateTime.Now.AddSeconds(CACHE_Min), System.Web.Caching.Cache.NoSlidingExpiration);
}
}
finally
{
rwlock.DowngradeFromWriterLock(ref lc);
}
}

return titlelist;

}
finally
{
rwlock.ReleaseReaderLock();
}
}

出的问题比较奇怪.有时候出现

1.此文档已具有 'XmlDeclaration' 节点。

2.有时候读取XML文件的方法出现获取为空,比如 GetUserInfo 方法 返回 Cls_UserInfo user 对象.这个user有时候就为空.

GetUserTitleInfo 方法 返回List<Cls_Title> titlelist,这个titlelist就为空.

有时候出现一次,刷新一次又好了..一直找不到问题所在..

方法里面的缓存键也是唯一的,根据不同的方法名,用户名,和配置文件名称组成的缓存键..



中间的 for (int i = 0; i < 5; i++)
也是后来弄的没办法了才加上去的,后来报错次数比以前少了,不过还是会有报.
...全文
355 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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