关于XML串行化和反串行化的问题!!!急
我在项目文件的web.config文件中添加
<appSettings>
<add key="News_SettingsFile" value="~/Config/News.Config"/>
</appSettings>
该文件指向
<?xml version="1.0"?>
<ModuleSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ConnectionString>server=10.80.162.87;database=NewsRelease;uid=newsadmin;pwd=newsadmin123;</ConnectionString>
<NewsUrl>http://localhost/test/modules/newsmanager/shownews.aspx</NewsUrl>
<AbstractLength>150</AbstractLength>
</ModuleSettings>
test类的GetSettings()方法
public static ModuleSetgings GetSettings()
{
HttpContext context = HttpContext.Current;
ModuleSetgings data = (ModuleSetgings)context.Cache["News_Settings"];
if (data == null)
{
XmlSerializer serializer = new XmlSerializer(typeof(ModuleSetgings));
try
{
string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
FileStream fs = new FileStream(fileName, FileMode.Open);
data = (ModuleSetgings)serializer.Deserialize(fs);
fs.Close();
context.Cache.Insert("News_Settings", data, new CacheDependency(fileName));
}
catch (System.IO.FileNotFoundException)
{
data = new ModuleSetgings();
}
}
return data;
}
///////////////////////////////////////////////////////////////////////////
将要串行化的 ModuleSetgings类
public class ModuleSetgings
{
private string connectionString;
private string newsUrl;
private int abstractLength;
[XmlElement]
public string ConnectionString
{
get { return connectionString; }
set { connectionString = value; }
}
[XmlElement]
public string NewsUrl
{
get { return newsUrl; }
set { newsUrl = value; }
}
[XmlElement]
public int AbstractLength
{
get { return abstractLength; }
set { abstractLength = value; }
}
}
我在另一类中调用他们,现在
提示错误:
“/WebSitePut”应用程序中的服务器错误。
--------------------------------------------------------------------------------
不应有 <ModuleSettings xmlns='http://schemas.microsoft.com/.NetConfiguration/v2.0'>。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.InvalidOperationException: 不应有 <ModuleSettings xmlns='http://schemas.microsoft.com/.NetConfiguration/v2.0'>。
源错误:
行 55: string fileName = HttpContext.Current.Server.MapPath(GetSettingsFile());
行 56: FileStream fs = new FileStream(fileName, FileMode.Open);
行 57: data = (ModuleSetgings)serializer.Deserialize(fs);
行 58: fs.Close();
行 59: context.Cache.Insert("News_Settings", data, new CacheDependency(fileName));
源文件: f:\WebSitePut\App_Code\News.Configuration\Configuration.cs 行: 57