I had the same problem with a data feed I had to process. The namespace thing had stumped and any docs/examples I found were no help.
I found and used the trial version of 'Liquid XML Studio' (http://www.liquid-technologies.com/). It generates a code snippet to start parsing xml. Dim nsMgr As Xml.XmlNamespaceManager ' after xmlNav = xpathDoc.CreateNavigator() nsMgr = New Xml.XmlNamespaceManager(xmlNav.NameTable) nsMgr.AddNamespace("ns0", "http://www.buylink.com.au") and update the select statement to use the namespace manager xmlNI = xmlNav.Select("/ns0:PurchaseOrderSent/DownloadPurchaseOrder/PurchaseOrde r/Header/@PoNo", nsMgr) David From: [email protected] [mailto:[email protected]] On Behalf Of Anthony Sent: Wednesday, 10 November 2010 2:50 p.m. To: 'ozDotNet' Subject: vb.net xml help I am using Xpath to get a value but fails when the xml has an element of the form <ns0:PurchaseOrderSent xmlns:ns0="http://www.buylink.com.au"> </ns0:PurchaseOrderSent> BUT works when its of the form <ns0 xmlns:ns0="http://www.buylink.com.au"></ns0 > Anyone see the issues? Is <ns0:PurchaseOrderSent.. valid XML element? FAILS <ns0:PurchaseOrderSent xmlns:ns0="http://www.buylink.com.au"> <DownloadPurchaseOrder> <PurchaseOrder> <Header DocType="PO" PoNo="2192" /> </PurchaseOrder> </DownloadPurchaseOrder> </ns0:PurchaseOrderSent> Function getXMLXpath(ByVal sXML As String) As String Dim myEncoder As New System.Text.ASCIIEncoding Dim bytes As Byte() = myEncoder.GetBytes(sXML) Dim ms As MemoryStream = New MemoryStream(bytes) Dim xpathDoc As XPathDocument Dim xmlNav As XPathNavigator Dim xmlNI As XPathNodeIterator xpathDoc = New XPathDocument(ms) xmlNav = xpathDoc.CreateNavigator() Try xmlNI = xmlNav.Select("/ns0:PurchaseOrderSent/DownloadPurchaseOrder/PurchaseOrde r/Header/@PoNo") //Excepytion Erros 'Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.' While (xmlNI.MoveNext()) Return xmlNI.Current.Value End While Catch ex As Exception End Try End Function WORKS <ns0 xmlns:ns0="http://www.buylink.com.au"> <DownloadPurchaseOrder> <PurchaseOrder> <Header DocType="PO" PoNo="2192" /> </PurchaseOrder> </DownloadPurchaseOrder> </ns0 > Function getXMLXpath(ByVal sXML As String) As String Dim myEncoder As New System.Text.ASCIIEncoding Dim bytes As Byte() = myEncoder.GetBytes(sXML) Dim ms As MemoryStream = New MemoryStream(bytes) Dim xpathDoc As XPathDocument Dim xmlNav As XPathNavigator Dim xmlNI As XPathNodeIterator xpathDoc = New XPathDocument(ms) xmlNav = xpathDoc.CreateNavigator() Try xmlNI = xmlNav.Select("/ns0/DownloadPurchaseOrder/PurchaseOrder/Header/@PoNo") While (xmlNI.MoveNext()) Return xmlNI.Current.Value 'Returns 2192 End While Catch ex As Exception End Try End Function Is your website being IntelliXperienced? <http://www.intellixperience.com/signup.aspx> | www.yougoingmyway.com ? regards Anthony (*12QWERNB*) Is your website being IntelliXperienced?
