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

  • Home
  • SEARCH
  • 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 7938857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:55:28+00:00 2026-06-03T22:55:28+00:00

this code works perfectly. Creates an XDocument, puts it in cache, and retrieves it

  • 0

this code works “perfectly”. Creates an XDocument, puts it in cache, and retrieves it the next time round.

BUT i’d like the object in cache NOT to be affected by the changes made after the marked place, where I add some elements only valid in this session.

The object in cache is relevant for all users on the page, so should only be genereted/altered once in cache lifetime.

I COULD clone it from the cache or store it as a string I guess (not a reference type) and losd into new XDocument, but what would be the most gentle way? Seems overkill, when I can cache the XDocument as it is…

Thanks in advance,

Steen

{
            string cachekey = "categories";
            var cateGoryDoc = HttpRuntime.Cache.Get(cachekey) as XDocument;

            if (cateGoryDoc == null)
            {
                XDocument doc = XDocument.Parse(this.ProductList1.getXML());
                IEnumerable<XElement> mainCats =
                    from Category1 in doc.Descendants("product").Descendants("category") select Category1;
                var cDoc = new XDocument(new XDeclaration("1.0", "utf-8", null), new XElement("root"));
                cDoc.Root.Add(mainCats);
                cateGoryDoc = cDoc;
                HttpRuntime.Cache.Insert(cachekey, cDoc, null,
                                             DateTime.Now.AddMinutes(10),
                                             TimeSpan.Zero);
            }


            // I DO NOT WANT TO CHANGE THE DOC IN CACHE!!
            if (HttpContext.Current.Request["tCats"] != null)
            {
                string[] tCats = HttpContext.Current.Request["tCats"].Split(Convert.ToChar(","));

                foreach (string tCat in tCats)
                {
                    cateGoryDoc.Root.Add(new XElement("selectedCategory", tCat));
                }
            }

            this.catecoryXML.DocumentContent = cateGoryDoc.ToString();
            this.catecoryXML.TransformSource = "/XSLTFile1.xslt";
        }
  • 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-03T22:55:29+00:00Added an answer on June 3, 2026 at 10:55 pm

    Cloning it is the way to go.

    You do this as follows:

    XDocument cachedDocument = ... get it from the cache ...
    XDocument clone = new XDocument(cachedDocument);
    ... modify the clone
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code that works perfectly to post one object to my server.
This code works perfectly in Ubuntu, Windows, and Mac OS X. It also works
This code works perfectly except when you click on a link, The page is
I have this code to write to a file, it works perfect but I
This code works great for generating thumbnails, but when given a very large (100MB+)
I'm using this for my code, it outputs to the xml file perfectly, but
For some reason my code works perfectly for Froyo(API 8) but gives me a
I'm doing something with XML and now I'm confused. This code works perfectly: QDomElement
This code works perfectly except for a small formatting issue I can't find an
Using this code to zip a folder and it works perfect on small files

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.