Wanting to read XML blob from configuration

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm probably missing something because the .NET configuration model seems
pretty screwed up. Since the configuration file is an XML file it would seem
to make sense, at least to me, to have the reading/writing of configuration
information rely on the XmlSerializer. Instead they came up with a whole new
set of configuration attributes to do, I guess, the same (or similar) thing
the XmlSerialization attributes do. Why?

Getting to my question at hand, I plan to use the XmlSerializer to read data
from my application configuration file. I would like to be able to request a
section and get back an XmlNode for that section. I will then deserialize
that using the XmlSerializer. I was hoping that I could simply call
ConfigurationManager.GetSection(...) passing in my section name and get back
an XmlNode. However, that doesn't work. So after looking at all the derived
ConfigurationSection classes it appears as if the DefaultSection class will
allow me to get an XML string which represents the section. Is this true? I
was hoping to get an XmlNode because I was thinking that the framework had it
in that form and didn't want to have to serialize back to a string.

I was also surprised that there doesn't seem to be any way to
programmatically specify a configSection. I seems as if I'm stuck having to
do the following:

<configuration>
<configSections>
<section name="orderFill" type="System.Configuration.DefaultSection,
System.Configuration, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
....
</configuration>

It's kind of ugly and if I know in the code that I want to use the
DefaultSection why can't I somehow tell the framework at runtime that I want
to add a new configSection removing the need to specify it in the application
configuration file?

--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
Hello Nick,

From your description, you're wantting to programmtically load the
application's app.config file(exe.config or web.config) and modify some
sections/values in it or add some new custom sections, correct?

Regarding on the new ConfigurationManager and type ConfigurationSection
model in .net framework 2.0, it is designed for encapsulating the
underlying XML processing on the raw app.config file. There are serveral
reasons such model will help for our configuration management:

** Given such a set of configuration class, you can read and write
configuration information without caring about the underlying persistence
format(xml or normal text or binary file...), we just use the given
configurationManager and the related classes to access configuration
settings in our code.

** .NET application's configuration setting is of a hierarchical model,
there are global settings (from machine.config or super level web.config
for ASP.NET application). Thus, when you use ConfigurationManager class to
open a configuration, it has helped you query and merge all the settings
from global to your local settings (no only the settings in your
application's own app.config file). This is also better than we use
file/XML I/O to read the app.config file directly(in such cases, we only
get the flat local configuration setting).

Of course, you can both modify existing configuration settings(first find
the certain Section from Configuration.Sections or SectionGroups) or add
new custom configuration section/sectiongroup. here is simple code snippet
demonstrate this:

============================
static void AddSection()
{
Configuration config =
ConfigurationManager.OpenExeConfiguration("ProtectConfigConsole.exe");

//add a new sectiongroup first if you want
ConfigurationSectionGroup group = new
ConfigurationSectionGroup();


config.SectionGroups.Add("customSectionGroup", group);

//my custom section class
MyCustomSection custsection = new MyCustomSection();
custsection.MyAttrib1 = "abc";
custsection.MyChildSection = new MyChildConfigElement("attr1",
"attr2");


group.Sections.Add("customSection", custsection);


config.Save(ConfigurationSaveMode.Full);

}
==============================

In addition, if you want to manipulate the certain section through XML
format, you can still get it via "Section.SectionInformation.GetRawXml()"
method. Though it return string format, you can simply use XmlDocument to
load it and get the root Element and use any XML dom API on it.
e.g.

==========================
static void RunConfigData()
{
Configuration config=
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection("appSettings");

XmlDocument doc = new XmlDocument();

doc.LoadXml(section.SectionInformation.GetRawXml());

Console.WriteLine(doc.DocumentElement.OuterXml);

}

=============================
After that, you can use "section.SectionInformation.SetRawXml" to update
the section by raw xml also.


Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
That's not what I was looking for. I don't need to modify the configuration
at runtime. What I want to do is:

1. Get the XML for a section. As I said I think the .NET model is brain
dead and using the XML Serializer would have been much better, but that's
another debate. I plan on using the XML Serializer to deserialize the
configuration into a class. How can I get the XML for a section using the
..NET 2.0 configuration model? I tried calling GetRawXml() but that fails
because I'm not in design mode.

2. I would rather not have to add a <configSections> section to my
application configuration. I would rather at runtime tell the configuration
manager what class will handle my section. But I don't see anyway to do this.
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
Thanks for your reply Nick,

For the further two problems you mentioned:


1. Get the XML for a section. As I said I think the .NET model is brain
dead and using the XML Serializer would have been much better, but that's
another debate. I plan on using the XML Serializer to deserialize the
configuration into a class. How can I get the XML for a section using the
.NET 2.0 configuration model? I tried calling GetRawXml() but that fails
because I'm not in design mode.
==============================
For the "GetRawXml" method, it does has some code check internal that check
the design-time property and from the document, it also indicate that this
method is used by .net runtime internal and not intended to use by
developers directly. Therefore, for getting raw XML, you may need to use
XML api directly.

However, I'm still wondering what would you need to do through raw XML
parsing? What classes will you use to associate those configuration XML
content through XMLSerializer? The .net framework 2.0 has already provided
strong-typed classes that can help read and write configuration sections in
app or web.config file .e.g
System.Configuration.AppSettingsSection
System.Configuration.ConnectionStringsSection
System.Net.Configuration.AuthenticationModulesSection
System.Web.Configuration.AnonymousIdentificationSection
System.Web.Configuration.MachineKeySection
System.Net.Configuration.WebRequestModulesSection
System.Net.Configuration.SmtpSection
<<<<<<<<<<<<<<<<<<

also, these class also use XML API to parse the configuration file when get
initialized.




2. I would rather not have to add a <configSections> section to my
application configuration. I would rather at runtime tell the
configuration
manager what class will handle my section. But I don't see anyway to do
this.
=================================
the <configSections> element is used to provide the definition and handler
information of any custom/new configuration sections. This is necessary for
a section to be avalid in .net application config file. If you put a custom
section without definition in <configSections>, the .net runtime won't be
able to recognize and read it(since you haven't provided the handler class
for the section). Also such a configuration section with out
<configSections> definition is just like a normal XML fragment which is not
necessary to be saved in application config file. Is this what you want?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
I appears I'm not explaining this well enough. I've got my own configuration
information. I don't want to read any of .NET's configuration information.
It's information for my application. Therefore there are no existing .NET
configuration classes which will give me the information I'm looking for
(unless I want to store my configuration in the "appsettings", which I don't).

I plan to use the XmlSerializer to deserialize my configuration section into
an instance of my configuration class. I don't want to use .NET 2.0's
configuration model, I don't like it. Therefore I want to be able to specify
a configuration section and get back the XML for that section which I can
then use to deserialize into an instance of my configuration object. For
instance, I would ideally like to be able to do the following:

XmlSerializer serializer;
XmlNode node;
MyClass myClass;

serializer = new XmlSerializer(typeof(MyClass));
node = ConfigurationManager.GetSection("<my section name>");
myClass = (MyClass) serializer.Deserialize(new XmlNodeReader(node));

--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
Thanks for your reply Nick,

I know that you have custom data, the reasonly I suggest the .NET
configuration model is because you want to save them into app.config. If
you do not want to use the .NET configuration model, why do you choose the
app.config file? I think a custom xml file would be rather more flexible
and convenient to manipulate through XML Serializer or XML API. If you
want to store your custom data into app.config(within a custom section),
you need to use the .NET defined programming interface for developing
custom section in app.config. And in 2.0, there support wo models of custom
section handler:


#How to: Create Custom Configuration Sections Using
IConfigurationSectionHandler
http://msdn2.microsoft.com/en-us/library/ms228056.aspx

#How to: Create Custom Configuration Sections Using ConfigurationSection
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx


For your scenario, I think the first handler model will be abit more close
to what you want to do(directly access and manipulate the raw XML).

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
So it sounds like you're saying that if I want raw XML from a .NET app config
file I'm out of luck with .NET 2.0. This is just a guess, but I would say
you're probably wrong. I'm guessing there is a way to get the XmlNode
associated with a configuration section. I guess I'll have to research it
more on my own.
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
Thanks for your reply Nick,

I don't mean that you can not get RawXml (such a xml node) through .net
configuration API, actually you need to at least adopt some of the
configuration model, you need to create a sectionhandler class as below:


#How to: Create Custom Configuration Sections Using
IConfigurationSectionHandler
http://msdn2.microsoft.com/en-us/library/ms228056.aspx


this class can help you interact with raw XML when processing your custom
XML section in app.config file.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top