Need XPath help

G

Guest

Hi,

I am trying to use an XmlDocument object to manipulate the default database
instance node of the enterprise template library dataConfiguration section
from a custom installer step:

<?xml version="1.0" encoding="utf-8"?>
<dataConfiguration>
<xmlSerializerSection
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.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="AdessoOdbc"
xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
<databaseTypes>

I have tried several XPath queries but I am not having any luck fetching the
"defaultInstance" attribute - xpath seems to be unhappy about the '.' in the
"enterpriseLibrary.databaseSettings" node name. For example this code:

XmlNode node =
xml.SelectSingleNode("dataConfiguration/xmlSerializerSection/enterpriseLibrary.databaseSettings");

does NOT return "enterpriseLibrary.databaseSettings" node - it returns null.
Any ideas on how I can get to the defaultInstance attribute?
 
W

Wiebe Tijsma

Richard said:
Hi,

I am trying to use an XmlDocument object to manipulate the default database
instance node of the enterprise template library dataConfiguration section
from a custom installer step:

<?xml version="1.0" encoding="utf-8"?>
<dataConfiguration>
<xmlSerializerSection
type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,
Microsoft.Practices.EnterpriseLibrary.Data, Version=1.1.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="AdessoOdbc"
xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data">
<databaseTypes>

I have tried several XPath queries but I am not having any luck fetching the
"defaultInstance" attribute - xpath seems to be unhappy about the '.' in the
"enterpriseLibrary.databaseSettings" node name. For example this code:

XmlNode node =
xml.SelectSingleNode("dataConfiguration/xmlSerializerSection/enterpriseLibrary.databaseSettings");

does NOT return "enterpriseLibrary.databaseSettings" node - it returns null.
Any ideas on how I can get to the defaultInstance attribute?
Hi,

It's not the dot.

The enterpriseLibrary.databaseSettings is in a different namespace,
declared by the
xmlns="http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data"
attribute.

You have to use a nametable/namespacemanager to assign a prefix for this
namespace, after that you can use xpath queries it like this:

XmlNode node =
xml.SelectSingleNode("dataConfiguration/xmlSerializerSection/myprefix:enterpriseLibrary.databaseSettings");

If you need help with the C# part, let me know...

Luck, Wiebe
 
A

Abubakar

Hi,
Will this be the right way to do this? :

XmlTextReader xreader = new XmlTextReader( xmlfilename );
XmlNamespaceManager nsm = new XmlNamespaceManager(xreader.NameTable );
nsm.AddNamespace("abc",
"http://www.microsoft.com/practices/enterpriselibrary/08-31-2004/data");
XmlNode node =
doc.DocumentElement.SelectSingleNode("xmlSerializerSection/abc:enterpriseLib
rary.databaseSettings", nsm);

and than you can write:

node.Attributes.GetNamedItem("defaultInstance").Value

to get the value.

Ab.
http://joehacker.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top