By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,278 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,278 IT Pros & Developers. It's quick & easy.

SelectSingleNode not finding nodes

P: n/a
I have the following Visual Basic .NET code in an ASP.NET project:

' ...

Dim xdStructure As XmlDocument
Dim xnStructureRoot As XmlNode

xdStructure = New XmlDocument
xdStructure.Load(Server.MapPath("~/xml/Structure.xml"))

xnStructureRoot = xdStructure.DocumentElement
If xnStructureRoot Is Nothing Then
Throw New Exception("No document element found in
Structure.xml.")
End If

' Get application node.
xnApplication =
xnStructureRoot.SelectSingleNode("application[@tag='" & Name & "']")
If xnApplication Is Nothing Then
Throw New Exception("Unrecognized application '" & Name &
"'.")
End If

' ...

'Name' resolves to the name of an individual application within the
project defined by the 'application' node witrh attribute 'tag' equal
to 'Name.'

This code worked flawlessly until yesterday, properly finding the
right 'application' node and storing it in 'xnApplication'. Now the
SelectSingleNode call in the last group no longer returns the node
requested; it returns Nothing and the "unrecognized application"
exception gets thrown.

I have verified that the XML file loads and the node in question
exists. I can iterate For Each node In xnStructureRoot.ChildNodes and
see the 'application' nodes with their 'tag' attributes set. I have
added an AddNamespace call; no joy. I have no idea why this code, that
worked fine just last week, is not working now and would appreciate
any and all help.
Nov 12 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Please provide a sample of the XML.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jay Bienvenu" <sp**@bienvenu.net> wrote in message
news:61**************************@posting.google.c om...
I have the following Visual Basic .NET code in an ASP.NET project:

' ...

Dim xdStructure As XmlDocument
Dim xnStructureRoot As XmlNode

xdStructure = New XmlDocument
xdStructure.Load(Server.MapPath("~/xml/Structure.xml"))

xnStructureRoot = xdStructure.DocumentElement
If xnStructureRoot Is Nothing Then
Throw New Exception("No document element found in
Structure.xml.")
End If

' Get application node.
xnApplication =
xnStructureRoot.SelectSingleNode("application[@tag='" & Name & "']")
If xnApplication Is Nothing Then
Throw New Exception("Unrecognized application '" & Name &
"'.")
End If

' ...

'Name' resolves to the name of an individual application within the
project defined by the 'application' node witrh attribute 'tag' equal
to 'Name.'

This code worked flawlessly until yesterday, properly finding the
right 'application' node and storing it in 'xnApplication'. Now the
SelectSingleNode call in the last group no longer returns the node
requested; it returns Nothing and the "unrecognized application"
exception gets thrown.

I have verified that the XML file loads and the node in question
exists. I can iterate For Each node In xnStructureRoot.ChildNodes and
see the 'application' nodes with their 'tag' attributes set. I have
added an AddNamespace call; no joy. I have no idea why this code, that
worked fine just last week, is not working now and would appreciate
any and all help.

Nov 12 '05 #2

P: n/a


Jay Bienvenu wrote:
I have the following Visual Basic .NET code in an ASP.NET project: xnApplication =
xnStructureRoot.SelectSingleNode("application[@tag='" & Name & "']")
If xnApplication Is Nothing Then
Throw New Exception("Unrecognized application '" & Name &
"'.")
End If

' ...

'Name' resolves to the name of an individual application within the
project defined by the 'application' node witrh attribute 'tag' equal
to 'Name.'

This code worked flawlessly until yesterday, properly finding the
right 'application' node and storing it in 'xnApplication'. Now the
SelectSingleNode call in the last group no longer returns the node
requested; it returns Nothing and the "unrecognized application"
exception gets thrown.

I have verified that the XML file loads and the node in question
exists. I can iterate For Each node In xnStructureRoot.ChildNodes and
see the 'application' nodes with their 'tag' attributes set. I have
added an AddNamespace call; no joy. I have no idea why this code, that
worked fine just last week, is not working now and would appreciate
any and all help.


We need to see the relevant XML to find out whether that XPath selects a
node or not, in particular it is important to see the namespaces in use.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #3

P: n/a
This is proprietary information, plus it is rather long. So I can
only show a sample of the XML file.

[begin]
<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="Structure.xsd">
<version>0.3</version>
<application id="1" tag="MTL">
<name>MTL Entry</name>
<required></required>
<modes>
[...]
</modes>
</application>
</project>
[end]

There are multiple 'application' nodes within the 'project' node. The
XSD was generated via the "Create Schema" command in Visual Studio.
The XML structure has not changed since I created the schema. I added
a call to AddNamespace to the routine and still get the bug.
Nov 12 '05 #4

P: n/a
You will need a namespace manager for the XPath evaluation.
Something like this should do it:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdStructure.NameTable)
nsmgr.AddNamespace(string.Empty, "Structure.xsd")
xnApplication =
xnStructureRoot.SelectSingleNode("application[@tag='" & Name & "']", nsmgr)

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Jay Bienvenu" <sp**@bienvenu.net> wrote in message
news:61**************************@posting.google.c om...
This is proprietary information, plus it is rather long. So I can
only show a sample of the XML file.

[begin]
<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="Structure.xsd">
<version>0.3</version>
<application id="1" tag="MTL">
<name>MTL Entry</name>
<required></required>
<modes>
[...]
</modes>
</application>
</project>
[end]

There are multiple 'application' nodes within the 'project' node. The
XSD was generated via the "Create Schema" command in Visual Studio.
The XML structure has not changed since I created the schema. I added
a call to AddNamespace to the routine and still get the bug.

Nov 12 '05 #5

P: n/a
This is proprietary information, plus it is rather long. So I can
only show a sample of the XML file.

[begin]
<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="Structure.xsd">
<version>0.3</version>
<application id="1" tag="MTL">
<name>MTL Entry</name>
<required></required>
<modes>
[...]
</modes>
</application>
</project>
[end]

There are multiple 'application' nodes within the 'project' node. The
XSD was generated via the "Create Schema" command in Visual Studio.
The XML structure has not changed since I created the schema. I added
a call to AddNamespace to the routine and still get the bug.
Nov 12 '05 #6

P: n/a


Jay Bienvenu wrote:
This is proprietary information, plus it is rather long. So I can
only show a sample of the XML file.

[begin]
<?xml version="1.0" encoding="utf-8" ?>
<project xmlns="Structure.xsd">
<version>0.3</version>
<application id="1" tag="MTL">
<name>MTL Entry</name>
<required></required>
<modes>
[...]
</modes>
</application>
</project>
[end]

There are multiple 'application' nodes within the 'project' node. The
XSD was generated via the "Create Schema" command in Visual Studio.
The XML structure has not changed since I created the schema. I added
a call to AddNamespace to the routine and still get the bug.


Well the problem is that XPath 1.0 doesn't know a default namespace so
your code is only going to work if it associates a prefix with the
namespace Structure.xsd and then uses that prefix in the XPath
expression passed to SelectSingleNode. Pseudo code:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdStructure.NameTable)
nsmgr.AddNamespace("prefix", "Structure.xsd")
xnApplication =
xnStructureRoot.SelectSingleNode("prefix:applicati on[@tag='" & Name &
"']", nsmgr)
--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.