Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / April 2006

Tip: Looking for answers? Try searching our database.

Problem getting node

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John Wilhelm - 21 Apr 2006 01:44 GMT
I'm having a problem in by VB.net 2005 application. When i try to get a node
from my app.config file the node come back with "nothing". The xmldocment
loads OK, but I can't retrive a node. The app.config file is listed below
and the code is listed below that, the line with "-->>" is where i try to
get the node. Can anyone see where I'm going wrong.

A dump of the xmlDocument is listed below the code.

Note: "MyKey" is set to "Reports"

Thank,
John
johnw@iirp.coedu.usf.edu

App.config file
----------------------------------------------------------------------------------
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
 <configSections>
 </configSections>
 <connectionStrings>
   <add name="My.MySettings.DataConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Ingredients_Data.mdb" providerName="System.Data.OleDb" />
 </connectionStrings>
 <appSettings>
   <add key="Reports" value="C:\current\Reports.xls" />
   <add key="Calendar" value="C:\current\Calendar.xls" />
   <add key="ClaimContinuum"
value="C:\current\ClaimContinuumReportBuild.xls" />
 </appSettings>
</configuration>
----------------------------------------------------------------------------------
----------------------------------------------------------------------------------

Public Function Save_Config_Parameter(ByVal MyKey As String, ByVal MyValue
As String) As String

Dim XmlDocument As New XmlDocument
Dim XmlNode As XmlNode
Dim XmlRoot As XmlNode
Dim XmlKey As XmlNode
Dim XmlValue As XmlNode

Save_Config_Parameter = ""
XmlDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)--->> XmlNode =XmlDocument.DocumentElement.SelectSingleNode("//configuration/appSettings/add[@key=""" & MyKey & """]") If XmlNode Is Nothing Then  ' The node does not exist, let's create it  XmlNode = XmlDocument.CreateNode(XmlNodeType.Element, "add", "")  ' Adding the Key attribute to the node, keep in mind, Xml tokens are casesensitive  ' We should use 'key' instead of 'Key'  XmlKey = XmlDocument.CreateNode(XmlNodeType.Attribute, "key", "")  XmlKey.Value = MyKey  XmlNode.Attributes.SetNamedItem(XmlKey)  ' Adding the key value, once again, remember that Xml tokens are casesensitive  XmlValue = XmlDocument.CreateNode(XmlNodeType.Attribute, "value", "")  XmlValue.Value = MyValue  XmlNode.Attributes.SetNamedItem(XmlValue)  ' Add the new node to the root  XmlRoot =XmlDocument.DocumentElement.SelectSingleNode("/configuration/appSettings")  If Not XmlRoot Is Nothing Then   XmlRoot.AppendChild(XmlNode)  Else   Save_Config_Parameter = "ERROR"  End If Else    '  ' The node exist, save the new value  '  XmlNode.Attributes.GetNamedItem("value").Value = MyValue End If XmlDocument.Save(Application.ExecutablePath & ".config") XmlDocument = NothingEnd Function=======================================================================>? XmlDocument{System.Xml.XmlDocument}    Attributes: Nothing    BaseURI: "file:///C:/John/bin/Debug/App.vshost.exe.config"    ChildNodes: {System.Xml.XmlChildNodes}    DocumentElement: {System.Xml.XmlElement}    DocumentType: Nothing    FirstChild: {System.Xml.XmlDeclaration}    HasChildNodes: True    Implementation: {System.Xml.XmlImplementation}    InnerText: ""    InnerXml: "<?xml version="1.0"?><configurationxmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"><configSections></configSec
tions><connectionStrings><addname="My.MySettings.DataConnectionString"connection
String="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Ingredients_Data.mdb
" providerName="System.Data.OleDb"/></connectionStrings><appSettings><add key="Reports"value="C:\current\Reports.xls" /><add key="Calendar"value="C:\current\Calendar.xls" /><add key="ClaimContinuum"value="C:\current\ClaimContinuumReportBuild.xls"/></appSettings></configuration>"    IsReadOnly: False    Item: In order to evaluate an indexed property, the property must bequalified and the arguments must be explicitly supplied by the user.    LastChild: {System.Xml.XmlElement}    LocalName: "#document"    Name: "#document"    NamespaceURI: ""    NameTable: {System.Xml.NameTable}    NextSibling: Nothing    NodeType: Document {9}    OuterXml: "<?xml version="1.0"?><configurationxmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"><configSections></configSec
tions><connectionStrings><addname="My.MySettings.DataConnectionString"connection
String="Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\Ingredients_Data.mdb
" providerName="System.Data.OleDb"/></connectionStrings><appSettings><add key="Reports"value="C:\current\Reports.xls" /><add key="Calendar"value="C:\current\Calendar.xls" /><add key="ClaimContinuum"value="C:\current\ClaimContinuumReportBuild.xls"/></appSettings></configuration>"    OwnerDocument: Nothing    ParentNode: Nothing    Prefix: ""    PreserveWhitespace: False    PreviousSibling: Nothing    SchemaInfo: {System.Xml.Schema.XmlSchemaInfo}    Schemas: {System.Xml.Schema.XmlSchemaSet}    Value: Nothing>
dickster - 21 Apr 2006 10:38 GMT
Hi John

The problem I believe lies in the fact that your XML falls under the
default namespace
xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"

And your xpath expression is not namespace aware.

That is why your Xpath expression doesn't work as you hope - i.e
XmlNode =
XmlDocument.DocumentElement.SelectSingleNode("//configuration/appSettings/add[@key="""
& MyKey & """]")

i.e this always sets XmlNode to null

A quick work around would be to remove the namespace from your Xml to
get your code rolling and xpath working.

- however as has been quoted elsewhere on this group
"Changing the XML to remove xmlns is usually not the right way to solve
the problem but rather an attempt to circumvent the problem."

For more info on namespaces & xpath in .NET read:
Article by Martin Honnen -
www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616
&
"XPath Queries with Namespaced Mapped Prefixes" in MSDN

Dickster

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.