all groups > dotnet xml > august 2005 >
You're in the

dotnet xml

group:

xpath against dataconfiguration.config


xpath against dataconfiguration.config news.microsoft.com
8/29/2005 4:11:01 PM
dotnet xml:
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


Re: xpath against dataconfiguration.config Pascal Schmitt
8/30/2005 12:00:00 AM
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 );


--
Re: xpath against dataconfiguration.config news.microsoft.com
8/30/2005 2:00:45 PM
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:
[quoted text, click to view]

AddThis Social Bookmark Button