Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:06:44+00:00 2026-06-07T16:06:44+00:00

I have a huge XML document which I have to parse it to generate

  • 0

I have a huge XML document which I have to parse it to generate domain objects.

Because the document is huge, i don’t want to parse it every time a user requests it, but only first time, then saving all the objects into cache.

public List<Product> GetXMLProducts()
{
    if (HttpRuntime.Cache.Get("ProductsXML") != null)
    {
        return (List<Product>)(HttpRuntime.Cache.Get("ProductsXML"));
    }

    string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Content\\Products.xml");
    XmlReader reader = XmlReader.Create(xmlPath);
    XDocument doc = XDocument.Load(reader);

    List<Product> productsList = new List<Product>();
    // Parsing the products element

    HttpRuntime.Cache.Insert("ProductsXML", productsList);
    return productsList;
}

How is the best way i can make this function working in singleton and be thread-safe?

Fixed the saving an object into cache method (was a copy-paste mistake)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:06:46+00:00Added an answer on June 7, 2026 at 4:06 pm

    Create a Lazy static and keep in memory for the lifetime of the application. And don’t forget the “true” part, that’s what makes it thread safe.

    public static readonly Lazy<List<Product>> _product = new Lazy<List<Products>>(() => GetProducts(), true);
    

    To add this to your model, just make it private and return _product.Value;

    public MyModel
    {
        ... bunch of methods/properties
    
        private static readonly Lazy<List<Product>> _products = new Lazy<List<Products>>(() => GetProducts(), true);
    
        private static List<Product> GetProducts()
        {
            return DsLayer.GetProducts();
    
        }
    
        public List<Product> Products { get { return _products.Value; } }
    }
    

    To create a singleton using Lazy<>, use this pattern.

    public MyClass
    {
        private static readonly Lazy<MyClass> _myClass = new Lazy<MyClass>(() => new MyClass(), true);
    
        private MyClass(){}
    
        public static MyClass Instance { get { return _myClass.Value; } }
    }
    

    Update/Edit:

    Another lazy pattern for use within a context (i.e. Session)

    Some Model that is saved in Session:

    public MyModel
    {
       private List<Product> _currentProducts = null;
       public List<Product> CurrentProducts 
       {
          get
          {
             return this._currentProducts ?? (_currentProducts = ProductDataLayer.GetProducts(this.CurrentCustomer));
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple but huge xml file like below. I want to parse
I have a huge XML document 200MB in size containing textual information. The data
I have one huge XML document. I have set of XSL representing each node
I have a huge XML files up to 1-2gb, and obviously I can't parse
I have a huge xml file and i want to convert it to a
I have got a huge xml document. something like that <?xml version=1.0 encoding=utf-8?> <elements>
I have a huge xml file (1 Gig). I want to move some of
I have an HTML file and i want to convert to XML document only
I have to read a huge xml file which consists of over 3 million
I have a thousands of data parsed from huge XML to be inserted into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.