关于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
...全文
348 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">
abstract (关键字) 抽象 ['æbstrækt] access vt.访问,存取 ['ækses]'(n.入口,使用权) algorithm n.算法 ['ælgәriðm] Annotation [java] 代码注释 [ænәu'teiʃәn] anonymous adj.匿名的[ә'nɒnimәs]'(义:directly adv.直接地,立即[di'rektli, dai'rektli]) apply v.应用,适用 [ә'plai] application n.应用,应用程序 [,æpli'keiʃәn]' (application crash 程序崩溃) arbitrary a.任意的 ['ɑ:bitrәri] argument n.参数;争论,论据 ['ɑ:gjumәnt]'(缩写 args) assert (关键字) 断言 [ә'sә:t] ' (java 1.4 之后成为关键字) associate n.关联(同伴,伙伴) [ә'sәuʃieit] attribute n.属性(品质,特征) [ә'tribju:t] boolean (关键字) 逻辑的, 布尔型 call n.v.调用; 呼叫; [kɒ:l] circumstance n.事件(环境,状况) ['sә:kәmstәns] crash n.崩溃,破碎 [kræʃ] cohesion 内聚,黏聚,结合 [kәu'hi:ʒәn] (a class is designed with a single, well-focoused purpose. 应该不止这点) command n. 命令,指令 [kә'mɑ:nd](指挥, 控制) (command-line 命令行) Comments [java] 文本注释 ['kɒments] compile [java] v.编译 [kәm'pail]' Compilation n.编辑[,kɒmpi'leiʃәn] const (保留字) constant n. 常量, 常数, 恒量 ['kɒnstәnt] continue (关键字) coupling 耦合,联结 ['kʌpliŋ] making sure that classes know about other classes only through their APIs. declare [java] 声明 [di'klєә] default (关键字) 默认值; 缺省值 [di'fɒ:lt] delimiter 定义符; 定界符 Encapsulation[java] 封装 (hiding implementation details) Exception [java] 例外; 异常 [ik'sepʃәn] entry n.登录项, 输入项, 条目['entri] enum (关键字) execute vt.执行 ['eksikju:t] exhibit v.显示, 陈列 [ig'zibit] exist 存在, 发生 [ig'zist] '(SQL关键字 exists) extends (关键字) 继承、扩展 [ik'stend] false (关键字) final (关键字) finally (关键字) fragments 段落; 代码块 ['frægmәnt] FrameWork [java] 结构,框架 ['freimwә:k] Generic [java] 泛型 [dʒi'nerik] goto (保留字) 跳转 heap n.堆 [hi:p] implements (关键字) 实现 ['implim

62,068

社区成员

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

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

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

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