你如何在C#(ASP.net)中使用SessionStateItemCollection

时间:2022-09-13 20:54:50

If I store the contents of an xml file (name -> value) in a session collection, whats the best way of accessing it on another page (i.e not the one that the below script runs on).

如果我将xml文件(name - > value)的内容存储在会话集合中,那么在另一个页面*问它的最佳方式是什么(即不是下面脚本运行的那个)。

Is it just a job of creating a new instance of SessionStateItemCollection on each page I want to access the collection?

它只是在我想要访问集合的每个页面上创建SessionStateItemCollection的新实例的工作吗?

System.Xml.XmlDocument oXmlDoc = new System.Xml.XmlDocument();

            oXmlDoc.Load(Server.MapPath("xml.xml")); //load XML file

            XmlNode root = oXmlDoc.DocumentElement;

            SessionStateItemCollection PSess = new SessionStateItemCollection(); //session collection

            for (int x = 0; x < root.ChildNodes.Count; x++) //loop through children and add to session
            {
                PSess[root.ChildNodes.Item(x).Name] = root.ChildNodes.Item(x).InnerText;  //add xml values to session 

            }

1 个解决方案

#1


If this is in ASP.NET then I don't see why you would create a new SessionStateItemCollection object. In this case as you would need to handle persisting this between pages yourself.

如果这是在ASP.NET中,那么我不明白为什么要创建一个新的SessionStateItemCollection对象。在这种情况下,您需要自己处理页面之间的持久性。

ASP.NET provides access to a SessionStateItemCollection object via the Session object of the Page or the HttpContext.Current object if you are wnating to use this from a class. You can store your key values pairs in there via the Session object and retrieve them from the Session object on subsequent pages.

ASP.NET提供了通过Page的Session对象或HttpContext.Current对象访问SessionStateItemCollection对象的权限,如果您希望从类中使用它。您可以通过Session对象将键值对存储在那里,并在后续页面上从Session对象中检索它们。

I would rewrite your code as follows

我会按如下方式重写您的代码

System.Xml.XmlDocument oXmlDoc = new System.Xml.XmlDocument();

oXmlDoc.Load(Server.MapPath("xml.xml")); //load XML file

XmlNode root = oXmlDoc.DocumentElement;

for (int x = 0; x < root.ChildNodes.Count; x++) //loop through children and add to session
{
    Session[root.ChildNodes.Item(x).Name] = root.ChildNodes.Item(x).InnerText;  //add xml values to session 
}

You can now access the contents from another page using the Session object (the following code will interate through everything store in the session):

您现在可以使用Session对象从另一个页面访问内容(以下代码将通过会话中的所有商店进行交互):

for (int i = 0; i < Session.Count; i++)
{
    Response.Write(Session.Keys[i] + "-" + Session[i].ToString());
}

#1


If this is in ASP.NET then I don't see why you would create a new SessionStateItemCollection object. In this case as you would need to handle persisting this between pages yourself.

如果这是在ASP.NET中,那么我不明白为什么要创建一个新的SessionStateItemCollection对象。在这种情况下,您需要自己处理页面之间的持久性。

ASP.NET provides access to a SessionStateItemCollection object via the Session object of the Page or the HttpContext.Current object if you are wnating to use this from a class. You can store your key values pairs in there via the Session object and retrieve them from the Session object on subsequent pages.

ASP.NET提供了通过Page的Session对象或HttpContext.Current对象访问SessionStateItemCollection对象的权限,如果您希望从类中使用它。您可以通过Session对象将键值对存储在那里,并在后续页面上从Session对象中检索它们。

I would rewrite your code as follows

我会按如下方式重写您的代码

System.Xml.XmlDocument oXmlDoc = new System.Xml.XmlDocument();

oXmlDoc.Load(Server.MapPath("xml.xml")); //load XML file

XmlNode root = oXmlDoc.DocumentElement;

for (int x = 0; x < root.ChildNodes.Count; x++) //loop through children and add to session
{
    Session[root.ChildNodes.Item(x).Name] = root.ChildNodes.Item(x).InnerText;  //add xml values to session 
}

You can now access the contents from another page using the Session object (the following code will interate through everything store in the session):

您现在可以使用Session对象从另一个页面访问内容(以下代码将通过会话中的所有商店进行交互):

for (int i = 0; i < Session.Count; i++)
{
    Response.Write(Session.Keys[i] + "-" + Session[i].ToString());
}