all groups > dotnet xml > july 2007 >
You're in the

dotnet xml

group:

xpath question with namespace



xpath question with namespace David Thielen
7/17/2007 10:28:03 AM
dotnet xml: Hi;

If I have the xml:
<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
....

What is the xpath in XmlDocument.SelectNodes() to get the root node? I
thought it was "/configuration" as the namespace is the default - but that
does not work.

If I get rid of the xmlns=... then it works fine.

What am I missing here?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Re: xpath question with namespace David Thielen
7/17/2007 11:58:10 AM
Hi;

Ok, so in this case everything is under that namespace and I need to use it
for all nodes?

Which brings up a new question. This is for an app.dll.config file. Very
weird case - it is for a Word AddIn and we are supposed to not use
winword.exe.config but instead create an addin.dll.config and read from that.
The problem is the xmlns=... may be in the file. I have seen config files
with it and config files without it. So How do I do a select that handles
both cases?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




[quoted text, click to view]
Re: xpath question with namespace Martin Honnen
7/17/2007 7:43:25 PM
[quoted text, click to view]

Choose a prefix for your elements e.g. nc, then use an XmlNamespaceManager
XmlNamespaceManager namespaceManager = new
XmlNamespaceManager(yourXmlDocumentInstance.NameTable);
namespaceManager.AddNamespace("nc",
"http://schemas.microsoft.com/.NetConfiguration/v2.0");
then use that prefix in your XPath expressions and pass in the namespace
manager as the second argument to SelectNodes/SelectSingleNode e.g.
yourXmlDocumentInstance.SelectSingleNode("/nc:configuration")

On the other hand
yourXmlDocumentInstance.DocumentElement
gives you the root element without using XPath.

--

Martin Honnen --- MVP XML
Re: xpath question with namespace v-wywang NO[at]SPAM online.microsoft.com
7/18/2007 12:00:00 AM
Hello Dave,
Thanks for Martin's suggestion.

SelectNodes() method Selects a list of nodes matching the XPath expression.
If the XPath expression does not include a prefix, SelectNodes() method is
assumed that the namespace URI is the empty namespace.(i.e. xmlns="") If
your XML includes a default namespace, we MUST use the XmlNamespaceManager
and add a prefix and namespace URI to it., just as Martin mentioned.

Otherwise, we should change the XPath expression to
XmlNodeList xnl =
yourXmlDocumentInstance.SelectNodes(@"*[local-name()='configuration']");
Or
Get the root element by the property of yourXmlDocumentInstance directly.
XmlElement configuration = yourXmlDocumentInstance.DocumentElement;
XmlElement configuration = yourXmlDocumentInstance["configuration"];

Hope this helps. Please let me know if you have any more concern. We are
glad to assist you.
Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Re: xpath question with namespace David Thielen
7/18/2007 9:42:01 AM
It's actually 3 nodes in and there can be multiple entries. So I guess I'll
try the select with the namespace and if that fails without.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




[quoted text, click to view]
Re: xpath question with namespace v-wywang NO[at]SPAM online.microsoft.com
7/19/2007 12:00:00 AM
Hello Dave,

For multiple entries, have you tried with Xpath
"*[local-name()='configuration']" ?
* will match all the namespace.
This method should work fine with/without namespace.

XmlNodeList xnl =
yourXmlDocumentInstance.SelectNodes(@"*@"*[local-name()='configuration']/*[l
ocal-name()='test']"");

Hope this helps.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Re: xpath question with namespace David Thielen
7/19/2007 9:24:02 AM
great idea - will do.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




[quoted text, click to view]
AddThis Social Bookmark Button