C# ConfigurationSection中的自定义xml反序列化/序列化(如使用IConfigurationSectionHandler)
我想在我的Web.config中使用我自己的自定义XML格式。在.NET1.1中,我将IConfigurationSectionHandler与XmlSerializer结合使用。由于IConfigurationSectionHandler已折旧,因此我希望对ConfigurationSection执行相同的操作。我这样试过:C# ConfigurationSection中的自定义xml反序列化/序列化(如使用IConfigurationSectionHandler),c#,.net,configuration,xml-serialization,C#,.net,Configuration,Xml Serialization,我想在我的Web.config中使用我自己的自定义XML格式。在.NET1.1中,我将IConfigurationSectionHandler与XmlSerializer结合使用。由于IConfigurationSectionHandler已折旧,因此我希望对ConfigurationSection执行相同的操作。我这样试过: protected override void DeserializeSection(System.Xml.XmlReader reader) {
protected override void DeserializeSection(System.Xml.XmlReader reader)
{
// my custom code to deserialize data from reader
}
public class MySection : ConfigurationSection
{
protected override void DeserializeSection(XmlReader reader)
{
// my code
}
}
<sectionGroup name="mySection" type="myNamspace.MySection, myAssembly"/>
<mySection>
<abc><xx/></abc>
</mySection>
如果我启动我的应用程序,我会得到一个解析器错误,它说:“无法识别的配置节mySection/customChildNode”。这在调用反序列化节之前发生。我做错了什么?有没有关于如何在ConfigurationSection中使用自定义xml的提示
编辑:按要求编辑一位mor代码
我试着这样实现我的部分:
protected override void DeserializeSection(System.Xml.XmlReader reader)
{
// my custom code to deserialize data from reader
}
public class MySection : ConfigurationSection
{
protected override void DeserializeSection(XmlReader reader)
{
// my code
}
}
<sectionGroup name="mySection" type="myNamspace.MySection, myAssembly"/>
<mySection>
<abc><xx/></abc>
</mySection>
并在Web.config中注册,如下所示:
protected override void DeserializeSection(System.Xml.XmlReader reader)
{
// my custom code to deserialize data from reader
}
public class MySection : ConfigurationSection
{
protected override void DeserializeSection(XmlReader reader)
{
// my code
}
}
<sectionGroup name="mySection" type="myNamspace.MySection, myAssembly"/>
<mySection>
<abc><xx/></abc>
</mySection>
然后我试着这样使用它:
protected override void DeserializeSection(System.Xml.XmlReader reader)
{
// my custom code to deserialize data from reader
}
public class MySection : ConfigurationSection
{
protected override void DeserializeSection(XmlReader reader)
{
// my code
}
}
<sectionGroup name="mySection" type="myNamspace.MySection, myAssembly"/>
<mySection>
<abc><xx/></abc>
</mySection>
我假设调用了ConfigurationSection的DeserializeSection,传入的XmlReader允许访问包含的自定义XML。但是没有调用DeserializeSection,并且出现了一个错误,表示mySection/abc未知。尝试使用
而不是我有一篇文章介绍了如何在不使用IConfigurationSectionHandler的情况下创建自定义配置节。请看一看athttp://devpinoy.org/blogs/jakelite/archive/2009/01/10/iconfigurationsectionhandler-is-dead-long-live-iconfigurationsectionhandler.aspx您将不得不显示更多的代码。请发布足够的代码让我们重现问题。嗨!你可以在这个链接上找到源代码。这里可以找到示例配置