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 / August 2005

Tip: Looking for answers? Try searching our database.

xpath against dataconfiguration.config

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
news.microsoft.com - 29 Aug 2005 22:11 GMT
Hi

I posted this question earlier today to microsoft.public.dotnet.vb.general
and microsoft.public.enterprise.tools.  My apologies if anyone reading this
has seen the message already.  I only just came across this forum while
searching again for the right place to post my question.

Okay, I have any application that uses the data access application building
block from Microsoft's Enterprise Library (patterns and practices site).  I
want to allow the users to select the database connection.  To do this, I
thought I could read the dataconfiguration.config file and obtain the name
attribute nodes from all the connectionString element nodes in the file.
Problem is, I can't get to them.  This may be due to a lack of experience
and knowledge using XPath.  I've tried a host of different XPath query
strings but I never get back more than one node and usually get zero nodes
when I should have two nodes.

Here is  my dataconfiguration.config file (relatively short).

<?xml version="1.0" encoding="utf-8"?>

<dataConfiguration>

<xmlSerializerSection
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null">

<enterpriseLibrary.databaseSettings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
defaultInstance="s09\Magic.prod"
xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">

<databaseTypes>

<databaseType name="Sql Server"
type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase,
Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null" />

</databaseTypes>

<instances>

<instance name="s11\eleven.prod" type="Sql Server"
connectionString="s11\eleven.prod" />

<instance name="s09\Magic.prod" type="Sql Server"
connectionString="s09\Magic.prod" />

</instances>

<connectionStrings>

<connectionString name="s11\eleven.prod">

<parameters>

<parameter name="database" value="jt1" isSensitive="false" />

<parameter name="Integrated Security" value="True" isSensitive="false" />

<parameter name="Max Pool Size" value="50" isSensitive="false" />

<parameter name="Min Pool Size" value="0" isSensitive="false" />

<parameter name="Connection Lifetime" value="30" isSensitive="false" />

<parameter name="Pooling" value="True" isSensitive="false" />

<parameter name="server" value="admsitsdbs11\eleven" isSensitive="false" />

</parameters>

</connectionString>

<connectionString name="s09\Magic.prod">

<parameters>

<parameter name="Connection Lifetime" value="30" isSensitive="false" />

<parameter name="database" value="s1" isSensitive="false" />

<parameter name="Integrated Security" value="True" isSensitive="false" />

<parameter name="Max Pool Size" value="5" isSensitive="false" />

<parameter name="Min Pool Size" value="0" isSensitive="false" />

<parameter name="Pooling" value="True" isSensitive="false" />

<parameter name="server" value="admsitsdbs09\magic" isSensitive="false" />

</parameters>

</connectionString>

</connectionStrings>

</enterpriseLibrary.databaseSettings>

</xmlSerializerSection>

</dataConfiguration>

Ok, and here is the code I'm trying to get to work.  I've included commented
out XPath strings I've tried, which didn't work.

Dim filename As String

Dim ds As DataSet

Dim dt As DataTable

Dim row As DataRow

Dim config As XmlDataDocument

Dim node As XmlNode

Dim nodeList As XmlNodeList

Dim element As XmlElement

Dim nodePath As String

Try

filename = Application.StartupPath + "\" + "dataconfiguration.config"

ds = New DataSet

'ds.ReadXmlSchema(filename)

ds.ReadXml(filename)

config = New XmlDataDocument(ds)

'config.Load(filename)

'nodePath =
"//dataConfiguration/xmlSerializerSection/enterpriseLibrary.databaseSettings/connectionStrings"

'nodePath =
"/dataConfiguration/xmlSerializerSection/enterpriseLibrary.databaseSettings/connectionStrings"

'nodePath =
"dataConfiguration/xmlSerializerSection/enterpriseLibrary.databaseSettings/connectionStrings"

'nodePath = "/dataConfiguration/xmlSerializerSection/*/connectionStrings"

'nodePath = "/*/*/*/connectionStrings"

'nodePath =
"descendant::dataConfiguration/xmlSerializerSection/enterpriseLibrary.databaseSettings"

'nodePath =
"descendant::xmlSerializerSection/enterpriseLibrary.databaseSettings"

'nodePath = "//connectionString"

nodePath = "/dataConfiguration/xmlSerializerSection/*/connectionStrings"

nodeList = config.SelectNodes(nodePath)

'nodeList =
config.ChildNodes(0).ChildNodes(0).ChildNodes(0).SelectNodes("//connectionStrings")

Catch ex As Exception

Throw ex

End Try

Any advice you can offer is appreciated!  Thanks in advance!

JB
Pascal Schmitt - 30 Aug 2005 00:09 GMT
Hello!

You forgot to consider the namespaces!

You need to "compile" your XPath-Expression to give it an
XmlNamespaceManager (untested, names may vary):

XPathExpression x =
config.Compile("/a:dataConfiguration/a:xmlSerializerSection/b:enterpriseLibrary.databaseSettings/b:connectionStrings");
XmlNamespaceManager m = new XmlNamespaceManager(config.NameTable);
m.AddNamespace("a", "");
m.AddNamespace("b", "http://...");
x.Context = m;
config.SelectNodes( x );

Signature

Pascal Schmitt

news.microsoft.com - 30 Aug 2005 20:00 GMT
Pascal

Thanks for the wake up call! I was trying to use the overload that doesn't
require the XMLNamespaceManager class and it was futile. As soon as I tried
using the other overload, BINGO, it worked as I expected from the beginning.
Thanks again, I was beating my head trying to determine where I was going
wrong.

John

in reply to:
> Hello!
>
[quoted text clipped - 10 lines]
> x.Context = m;
> config.SelectNodes( x );

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.