Connecting Tech Pros Worldwide Forums | Help | Site Map

Using XPath against serialized object

Don
Guest
 
Posts: n/a
#1: Nov 12 '05
Hi:

I have the following web service object serialized into my XML DOM. With
XPath, I am able to select '//document/Info/ID' from the XML. Adding the
namespace is where I get a problem(see XML below).
--------------------------
<document>
<Info xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID>416</LMID>
<NUM>1</BNUM>
</Info>

</document>

-------------------------------

Using the same XPath, nothing is returned. There are two ways I'd like to
try to solve this.

1) Try to remove the namespace (that seems to cause the problem)

2) Or make my XPath namespace aware.

<document>
<Info xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID
xmlns="http://web/DataSubSystemWebService/Services">416</LMID>
<NUM
xmlns="http://web/DataSubSystemWebService/Services">1</BNUM>

</Info>

</document>

-------------------------------

Thanks in advance,

Don



Derek Harmon
Guest
 
Posts: n/a
#2: Nov 12 '05

re: Using XPath against serialized object


"Don" <doduong12141214@hotmail.com> wrote in message news:O2zQahQ%23EHA.3840@tk2msftngp13.phx.gbl...[color=blue]
> I am able to select '//document/Info/ID' from the XML. Adding the
> namespace is where I get a problem(see XML below).[/color]

To make your XPath work with namespaces you must create an
XmlNamespaceManager and pass that as an argument to Select-
Nodes( ), there's an overload of that method that accepts an Xml-
NamespaceManager for this purpose.

Additionally, since you have declared default namespaces, you
must associate a prefix with the namespaces in the XPath query.
It's a practical matter -- b/c you can't type an empty string as a
namespace prefix in an XPath expression.

For example, in VB.NET,

' Given xmlDoc is an XmlDocument containing your XML.
'
Dim nsMan As XmlNamespaceManager = _
New XmlNamespaceManager( xmlDoc.NameTable)
nsMan.AddNamespace( "ns1", "http://web/DataSubSystemWebService/Services")
Dim nodes As XmlNodeList
nodes = xmlDoc.SelectNodes( "//document/Info/ns1:ID", nsMan)
'
' Enumerate over nodes with For Each or other loop to
' process each resulting XmlNode, if any.

See also the following article, I know you've overcome this before, :-)

http://groups-beta.google.com/group/...b?dmode=source


Derek Harmon


Closed Thread