perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
home » focus » msxml » ask a question » past questions & answers Friday, 12 October 2007
 
NEWS
MSXML 4.0 SP2 now available!

 
MSXML
Basics
DOM
SAX
XPath
XSLT
Schemas
SOM
HTTP Access
.NET
Data Islands
Ask a Question
   Past Q&As
C++ Samples
DLL/Version Info
Reference Guide
Books
KB Articles
   HOW TO
   SAMPLE
   INFO
   BUG/PRB
   FIX
   Misc.
MSXML Tips
   August 2002
   September 2002
MSXML Tools

Microsoft XML Core Services


Go back to list of previously asked questions and answers

Question: I am using ADO 2.6 to persist/save the recordset in the XML format (adPersistXML). Later on I use MSXML 4.0 to load this XML file and process it. I am using XPath expressions to select nodes, however I don't get back any nodes. Is it something to do with namespace prefixes?
Asked By: Kevin Marchand
Viewed: 2385
Answer: The ADO persisted XML uses namespaces such as xmlns:rs="urn:schemas-microsoft-com:rowset" and xmlns:z="#RowsetSchema" for data rows. You have to use setProperty "SelectionNamespaces" before selecting the nodes using MSXML DOM, and then use namespaces prefixes in your XPath expression.

Consider the following example:

Dim objADOConn As New ADODB.Connection
Dim objADORS As New ADODB.Recordset

objADOConn.Open "PROVIDER=SQLOLEDB.1;SERVER=.;UID=sa;PWD=;DATABASE=Northwind;"

objADORS.Open "SELECT * FROM Customers", objADOConn

objADORS.Save "c:\NWCustomers.xml", adPersistXML

objADORS.Close
Set objADORS = Nothing

objADOConn.Close
Set objADOConn = Nothing
The above Visual Basic ADO code connects to Northwind sample SQL Server database and saves data from the Customers table into a XML file called as c:\NWCustomers.xml.

Dim objXMLDoc As New MSXML2.DOMDocument40
Dim aNode As IXMLDOMNode

objXMLDoc.async = False
objXMLDoc.validateOnParse = False

If objXMLDoc.Load("c:\NWCustomers.xml") Then
    
    objXMLDoc.setProperty "SelectionNamespaces", _
        "xmlns:rs='urn:schemas-microsoft-com:rowset' " & _
        "xmlns:z='#RowsetSchema'"
    
    Set aNode = objXMLDoc.selectSingleNode("/xml/rs:data/z:row[@CustomerID='BERGS']")
    
    If aNode Is Nothing Then
        MsgBox "Not found!"
    Else
        iAttribCount = aNode.Attributes.length
        For iIndex = 0 To iAttribCount - 1
            MsgBox aNode.Attributes.Item(iIndex).nodeName & " : " & _
                aNode.Attributes.Item(iIndex).nodeTypedValue
        Next
    End If
    
    
Else
    'use parseError for error reporting
End If
The above MSXML code loads the XML file created by the prior VB ADO code. Note how the setProperty "SelectionNamespaces" is set and the namespaces prefixes used in the XPath expression. The above sample VB code searches for customer with ID equals BERGS; if found, displays all the attributes on that customer node.


Go back to list of previously asked questions and answers
  Contact Us |  | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy