此文档已具有 'XmlDeclaration' 节点
读取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++)
也是后来弄的没办法了才加上去的,后来报错次数比以前少了,不过还是会有报.