.net c# 一次加载 XML 文件的方法

发布于 2024-12-03 11:55:37 字数 436 浏览 0 评论 0 原文

实际上,我正在使用 xml 文件进行网页配置,xml 文件包含所有内容,所有控件/内容都是通过这些 XML 文件加载的,并且它可以工作,这些配置文件可以动态更改,而网页只需更改其布局/内容不需要重新编译/重新部署,我必须保持这种做事方式。

问题是我每页加载这些文件 20 次,当我想从我的类/ashx 文件访问它们时,我只是不断打开它们。有没有办法加载 xml 文件,因为它们是 globalresources ,并访问我的classes/cs/ashx 文件中的内容?

全局资源的问题是,如果将它们添加为嵌入的 资源,如果不重新编译它们,我就无法更改它们 这篇文章。如果我错了请纠正我。

谢谢。

Actually i'm using xml files for my web pages configuration , xml files contain everything and all the controls/content is loadded through theese XML files and it works , theese config files can change on the fly , and web pages just change their layout/content no need to recompile/redeploy , and i must keep this way of doing things.

The problem is that i'm loading these files like 20 times per page and when i want to access them from my classes/ashx files , i just keep opening them. Is there a way to load xml Files as they were globalresources , and access content from my classes/cs/ashx files?

the problem with global resources is that if add them as emmbded
resources , i can't change them without recompling them refering to
this post . Correct me if i'm wrong.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑忘罢 2024-12-10 11:55:37

您可以将文件存储在缓存会话应用程序ViewState 对象。

我认为最适合的是 Cache 对象,因为您可以添加一些基于文件的依赖项,和您的对象将自动更新:

Cache.Insert("CacheItem4", "Cached Item 4", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));

You can store your files in Cache, Session, Application or ViewState objects.

I think most preferably for is Cache object, because you can add some dependencies based on files, and your objects will be automatically updated:

Cache.Insert("CacheItem4", "Cached Item 4", new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")));
半仙 2024-12-10 11:55:37

在这种情况下,我使用一个助手:

public class CacheUtil
{
    private static readonly object locker=new object();
    public static T GetCachedItem<T>(string cacheKey,
                                     Func<T> valueCreateFunc, 
                                     TimeSpan duration)
    {
        var expirationTime = DateTime.UtcNow + duration;
        var cachedItem = HttpRuntime.Cache[cacheKey];
        if (cachedItem == null)
        {
            lock(locker)
            {
                cachedItem = HttpRuntime.Cache[cacheKey];
                if (cachedItem == null)
                {
                    cachedItem = valueCreateFunc();
                    HttpRuntime.Cache.Add(cacheKey,
                                          cachedItem,
                                          null,
                                          expirationTime,
                                          Cache.NoSlidingExpiration,
                                          CacheItemPriority.High,
                                          null);
                }
            }

        }
        return (T) cachedItem;
    }
}

我会使用这样的东西:

CacheUtil.GetCachedItem(
    "someUniqueKey",
    ()=>{ //fetch resource from disk
          return value;},
    TimeSpan.FromDays(1)
)

提供的委托每天只会被调用一次。如果该项目已在缓存中,则不会再次调用委托。

In situations like this, I use a helper:

public class CacheUtil
{
    private static readonly object locker=new object();
    public static T GetCachedItem<T>(string cacheKey,
                                     Func<T> valueCreateFunc, 
                                     TimeSpan duration)
    {
        var expirationTime = DateTime.UtcNow + duration;
        var cachedItem = HttpRuntime.Cache[cacheKey];
        if (cachedItem == null)
        {
            lock(locker)
            {
                cachedItem = HttpRuntime.Cache[cacheKey];
                if (cachedItem == null)
                {
                    cachedItem = valueCreateFunc();
                    HttpRuntime.Cache.Add(cacheKey,
                                          cachedItem,
                                          null,
                                          expirationTime,
                                          Cache.NoSlidingExpiration,
                                          CacheItemPriority.High,
                                          null);
                }
            }

        }
        return (T) cachedItem;
    }
}

which I would use something like this:

CacheUtil.GetCachedItem(
    "someUniqueKey",
    ()=>{ //fetch resource from disk
          return value;},
    TimeSpan.FromDays(1)
)

The supplied delegate will only be invoked once per day. If the item is already in cache, the delegate will not be invoked again.

葬花如无物 2024-12-10 11:55:37

您需要创建包装器来访问您的文件,该包装器将具有以下属性:

public class MyMarkupProvider
{
    public XDocument HomePageLayout {get;set;}
}

此外,还将创建它来缓存这些文件。了解如何使用缓存和文件 CacheDependency:使用 ASP.NET 缓存依赖项缓存文件

You need to create wrapper for accessing your files which will have properties like:

public class MyMarkupProvider
{
    public XDocument HomePageLayout {get;set;}
}

Also it would be create to cache these files. Take a look on using cache and file CacheDependency: Cache files using ASP.NET Cache Dependency

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文