我正在使用 Asp.net 3.5 和 C#
我必须将 XmlDocument 添加到我的应用程序状态,以便每次我的应用程序不访问文件系统上的 XML 文件时,我都会在 Global.asax.cs 的 Application_Start() 函数中添加它
我将此添加到系统状态:
protected void Application_Start(Object sender, EventArgs e)
{
string filePath = Server.MapPath("<path to my XML FILE>");
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlTickerDoc.Load(filePath);
}
finally
{
HttpContext.Current.Application["xmlDoc"] = xmlDoc;
}
}
在此代码中,我尝试加载 xml 文件,如果由于任何问题未加载该文件,那么我想要一个空 XmlDocument。
我以以下方式访问此 XmlDocument:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc = HttpContext.Current.Application["xmlDoc"];
我在构建时得到的错误是
无法将类型“object”隐式转换为“System.Xml.XmlDocument”。存在显式转换
那么如何将 HttpContext.Current.Application["xmlDoc"] 变量分配为 System.Xml.XmlDocument ?