关于XML串行化和反串行化的问题!!!急

oracleunix 2005-11-21 12:31:37
我在项目文件的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
...全文
349 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ghchen 2005-11-22
  • 打赏
  • 举报
回复
你肯定还没有改对呀
<ModuleSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

public class ModuleSetgings 默认情况下要匹配,如果你改对了就不会出<ModuleSettings xmlns=''>。这个提示要提示也是<ModuleSetgings xmlns=''>
把xml文件中的节点改成ModuleSetgings,记得还有个结束标记也要改</ModuleSetgings>
oracleunix 2005-11-21
  • 打赏
  • 举报
回复
我是在asp.net bet2中做的
oracleunix 2005-11-21
  • 打赏
  • 举报
回复
UP
oracleunix 2005-11-21
  • 打赏
  • 举报
回复
还是不行呀
还是会提示
不应有 <ModuleSettings xmlns=''>。
ghchen 2005-11-21
  • 打赏
  • 举报
回复
ModuleSetgings的节点名与ModuleSettings类名不相同

试试<ModuleSetgings xmlns:..
oracleunix 2005-11-21
  • 打赏
  • 举报
回复
gyf19(秋天的云):我试过了,会提示
不应有 <ModuleSettings xmlns=''>。
gyf19 2005-11-21
  • 打赏
  • 举报
回复
把<ModuleSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
变成<ModuleSettings>

gyf19 2005-11-21
  • 打赏
  • 举报
回复
不要<ModuleSettings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧