I'm having the darndest XML config file problem that I really need help with.
I'm supporting a .NET 1.1 desktop application with its own config file, and
I implement IConfigurationSectionHandler so I can have a custom config
section in my config file. The IConfigurationSectionHandler.Create method
just returns the 'section' XmlNode back to the consumer. The config file
looks kind of like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="MyConfigSettings" type="MyAssembly.ConfigReader, MyAssembly" />
</configSections>
<appSettings>
</appSettings>
<MyConfigSettings>
<ConfigSet zone="Test">
<add key="FirstKey" value="FirstValue"/>
<add key="SecondKey" value="SecondValue"/>
<add key="ThirdKey" value="ThirdValue"/>
</ConfigSet>
<ConfigSet zone="Integration">
<add key="FirstKey" value="FirstValue"/>
<add key="SecondKey" value="SecondValue"/>
<add key="ThirdKey" value="ThirdValue"/>
</ConfigSet>
</MyConfigSettings>
</configuration>
Then I've got this section of code that looks like this:
Public Shared Sub SetConfigZone(ByVal Zone As String)
Dim debug As String
Try
debug &= "Checking Zone variable " & Environment.NewLine
If (Zone Is Nothing) Then
MessageBox.Show("Zone variable passed in is nothing")
End If
debug &= "Getting XmlNode from config file " & Environment.NewLine
Dim parent As XmlNode =
ConfigurationSettings.GetConfig("MyConfigSettings")
debug &= "Checking parent variable " & Environment.NewLine
If (parent Is Nothing) Then
Throw New ConfigurationException("Unable to locate the
custom configuration section MyConfigSettings. Please check your config
file.")
End If
MessageBox.Show(parent.OuterXml)
debug &= "Beginning loop through nodes " & Environment.NewLine
For Each node As XmlNode In parent.ChildNodes
MessageBox.Show(node.OuterXml)
debug &= "Inspecting Node's zone attribute... " &
Environment.NewLine
If (node.Attributes("zone").Value = Zone) Then
debug &= "Correct node found... " & Environment.NewLine
debug &= "Creating new NameValueCollection... " &
Environment.NewLine
ConfigCommon.m_ConfigSettings = New NameValueCollection
debug &= "Looping through 'add' subnodes... " &
Environment.NewLine
For Each subNode As XmlNode In node.SelectNodes("add")
debug &= "Getting subnode's 'key' value... " &
Environment.NewLine
ConfigCommon.m_ConfigSettings.Add(subNode.Attributes("key").Value,
subNode.Attributes("value").Value)
Next
Return
End If
Next
Catch ex As Exception
MessageBox.Show(debug, "Our debug message")
MessageBox.Show(ex.ToString(), "Our error message")
End Try
'If we get this far, we haven't found our configuration section
Throw New ConfigurationException("Unable to locate configuration
zone " & Zone)
End Sub
So here's my problem. This code has been working just great for the last
six to nine months, but all of a sudden about two weeks ago all of the new
machine builds our desktop group puts out fail at the following line:
If (node.Attributes("zone").Value = Zone) //fails on first
run through
On new machines, it's producing a NullReferenceException. But when I take
the exact same code and run it on anyone else's machine, it works just fine.
In fact, you'll notice that I added a lot of little debugs and
MessageBox.Show statements in there to try to pinpoint it. When I run the
code on my machine, I notice that the MessageBox.Show(node.OuterXml) produces
the first node. When I run this on a newly built machine, the
MessageBox.Show produces an empty string.
My question is: what is missing from these new builds that was in the old
builds? I think I've ruled out different versions of the .NET framework.
Even if this was a breaking change in .NET 2.0, I tried forcing the .NET
Framework in the config file to 1.1 and still got the same error. And the
users definitely have the .NET 1.1 Framework.
Please help.
Thanks.
WenYuan Wang - 15 Mar 2007 14:49 GMT
Hi,
In fact, I have run the application with your code and .config file on my
machine (with .net framework 2.0), but without any error. The code snippet
you posted in newsgroup is fine.
> When I run the code on my machine, I notice that the
MessageBox.Show(node.OuterXml) >produces the first node. When I run this
on a newly built machine, the MessageBox.Show produces an empty string.
Is there any output by "MessageBox.Show(parent.OuterXml)" ?
I suggest you may output the parent.Attributes.count and through out each
Attribute of parent node rather than only output the attribute "zone".
There must be something wrong in Parent.Attributes.
Hope this helps,
Sincerely,
Wen Yuan
The Man From SQL - 15 Mar 2007 15:00 GMT
Hi Wen Yuan,
> Is there any output by "MessageBox.Show(parent.OuterXml)" ?
On both the old and new builds, the parent.OuterXml displays just fine.
This app is written in .NET 1.1 and the users have the .NET 1.1 Framework on
their machines. Does this code run just fine for you in .NET 1.1?
WenYuan Wang - 16 Mar 2007 12:30 GMT
Hello,
Thanks for your reply.
I uninstalled my .net framework 2.0 and installed .net framework 1.1 just
now. This is really a bug. I have reproduced this issue. The node.outXml
become null and I get the same exception as you.
But after I installed sp1 for .net frame work 1.1. The issue has gone away.
Apparently, this issue has been addressed in SP1 and .net framework 2.0.
(under my test).
For this reason, I suggest you may installed sp1 for .net 1.1 or installed
new .net 2.0 to resolved this issue.
You can download sp1 for .net 1.1 from the following website.
http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=A8F5
654F-088E-40B2-BBDB-A83353618B38
[Microsoft .NET Framework 1.1 Service Pack 1]
Hope this will hope you. Also if you have any further issue, please feel
free to let me know. I'm glad to assist you.
Have a great weekend.
Sincerely,
Wen Yuan
The Man From SQL - 16 Mar 2007 14:12 GMT
Wen Yuan,
Thank you very much for your help. Independently yesterday I stumbled
across this article which outlines my issue:
http://support.microsoft.com/kb/823054
As you indicated, it is a bug having do with Config files represented as
XML. I am working with our Desktop team to determine which patch installed
in the last few months fixed it, as I don't believe we are running SP1 but
have applied numerous patches from 1.1. Converting to .NET 2.0 is not an
option for us, as it would require much more thorough testing of our
application.
Thanks again for your help.
WenYuan Wang - 19 Mar 2007 08:04 GMT
Thanks for your feedback. You are welcome.:)
As you see, I think .NET 1.1 SP 1 will resolve the issue for you.
Have a great day,
Sincerely,
Wen Yuan