Session Variables - Saving with XML
page 2 of 4
by Keith Barrows
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 23096/ 47

Class - BaseSessionStorage

[Download Code]

The first thing we need to do is create the base classes we will use in the rest of the application.  The first base class is the XML Handler.  This class will handle all the data in and out of the Session Object.  So how do we do this?  If you look in BaseClasses\BaseSessionStorage.cs you will see we are using two protected objects and a protected string:

protected XmlDocument _internalState;
protected HttpContext _httpContext;
private string _identifier;

Now that we have defined our objects we need to start dealing with them.  _internalState is the actual XML object we use to handle all of our XML based tasks. _httpContext is an internal representation of the HTTP context the server deals with. And _identifier is the Session State key half of the Session State key/value pair.  Whenever any object is instantiated that is inheriting from this object we want to make sure that the bare minimums are always instantiated.  We do this by allowing only one constructor.

public BaseSessionStorage(HttpContext appContext, string Identifier, string BaseXml) 
    // Set internal objects and variables... 

The above constructor guarantees we get the 3 elements we need to make this class work.  The rest of this class is a Public Property to get the XML string and a small handful of Public Methods to Get and Set the values and to get a Count of XML Nodes.

This article is not meant to be an XML tutorial so I will not be going into the details of this base class.  Suffice it to say that the XML handling is straight forward and works.  Please feel free to step through it, tweak it if necessary and have fun with it.

With this base class we can now create the application classes that we will use in the Web Pages, Web Controls and Web Services.

View Entire Article

User Comments

Title: zzc   
Name: zcc
Date: 2012-08-01 6:53:38 AM
Title: Problem   
Name: Mohammad
Date: 2005-10-07 2:24:29 AM
sample cant run
Title: Are you nuts?   
Name: Curious
Date: 2004-12-22 9:50:48 PM
"Whatever the reasons, you need to save session state and are concerned about over-filling the memory."

So, you add on all the extra xml tags and the xmldocument with its collections of nodes to work with and that is somehow less memory intensive?

"Storing a full blown object in memory was not saving us much in the way of memory footprint."

Everything in .NET is an object/class. Strings included.

If you really needed to convert your session values to xml. You would create a class with properties that get/set your session variables in the get/set statements and convert the session values to the proper .NET datatype.

Then, use the System.Xml.Serialization.XmlSerializer to quickly and easily convert the class properties to an xml string.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-03-03 4:02:03 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search