Groups | Blog | Home
all groups > dotnet xml > january 2005 >

dotnet xml : Newbie question re column name does not belong to table item


coastalrocket NO[at]SPAM yahoo.co.uk
1/30/2005 5:02:25 AM
This is my first time with XML and first time with vb.Net so it's an
up hill learning curve for me but I would appreciate any help with
this probably simple request.

I've got a couple of column names in an xml source that my vb.net
application can't handle. I know its because there is a ":" in both
columns but i don't know of a way to treat the xml as a string, change
the column names and then feed the results to an xmltextreader.

regards,
AndyB

this is my xml feed: earthquake data from USGS.
http://earthquake.usgs.gov/recenteqsww/eqs1day-M2.5.xml

below is the error:
An unhandled exception of type 'System.ArgumentException' occurred in
system.data.dll
Additional information: Column 'geo:lat' does not belong to table
item.

below is the current code:
Const URLString As String =
"http://earthquake.usgs.gov/recenteqsww/eqs1day-M2.5.xml"
' We must be able to read the source as a string before it is read as
XML

' We need to replace geo:lat with latitude and
' geo:long with longitude
Dim reader As XmlTextReader = New XmlTextReader(URLString)
' I guess i need something here to take the reader and feed its
contents into a string
' do my replaces and then throw back to a XMLDataset?

Dim EarthquakesXMLDS
EarthquakesXMLDS = New DataSet
EarthquakesXMLDS.ReadXml(reader)
Dim EarthquakesXMLDT As DataTable = EarthquakesXMLDS.Tables("item")

Derek Harmon
1/30/2005 10:10:28 PM
[quoted text, click to view]

Instead of XmlTextReader, you can subclass XmlTextReader
and use that to read-in the XML document and perform the
replacements while the XML comes streaming in. Here's an
example of such a subclass.

- - - GRXmlTextReader.vb
Imports System
Imports System.IO
Imports System.Xml

Public Class GeoReplacementXmlTextReader
Inherits XmlTextReader

Public Sub New( ByVal source As TextReader)
MyBase.New( source)
End Sub

Public Overrides ReadOnly Property Prefix( ) As String
Get
Dim _prefix As String = MyBase.Prefix
If (_prefix = "geo") Then
Dim localName As String = MyBase.LocalName
If (localName = "lat") Then
Return ""
Else If (localName = "long") Then
Return ""
End If
End If
Return _prefix
End Get
End Property

Public Overrides ReadOnly Property LocalName( ) As String
Get
Dim prefix As String = MyBase.Prefix
Dim _localName As String = MyBase.LocalName
If (prefix = "geo") Then
If (_localName = "lat") Then
Return "latitude"
Else If (_localName = "long") Then
Return "longitude"
End If
End If
Return _localName
End Get
End Property

Public Overrides ReadOnly Property NamespaceURI( ) As String
Get
Dim nsURI As String = MyBase.NamespaceURI
If (nsURI = "http://www.w3.org/2003/01/geo/wgs84_pos#") Then
Dim localName As String = MyBase.LocalName
If (localName = "lat") Then
Return ""
Else If (localName = "long") Then
Return ""
End If
End If
Return nsURI
End Get
End Property

End Class
- - -

[quoted text, click to view]

Then the one-line replacement,

Dim reader As GeoReplacementXmlTextReader = _
New GeoReplacementXmlTextReader( _
New System.IO.StreamReader( URLString) )

You're also planning to replace the "http://purl.org/dc/elements/1.1/"
namespace URI prefix, right?


Derek Harmon

digger440uk
1/31/2005 12:40:51 PM
Cheers Derek. That is fantastic what you've posted. i'll try it out on
my commute tomorrow. since i'm clueless over the namespace question i
might be back here quite quickly.

But thanks again, its replies like this which blow me away sometimes.
Andy

[quoted text, click to view]
coastalrocket NO[at]SPAM yahoo.co.uk
2/5/2005 10:07:39 AM
Hi Derek,
I tried the code you posted but get an error of 'URI formats are not
supported.' when i declare
[quoted text, click to view]
It looks like a system.io.streamreader doesn't accept a URL string.
I'll try sorting this out but i thought i'd post back quickly with the
error in case anyone knows immediately the solution here.

[quoted text, click to view]
coastalrocket NO[at]SPAM yahoo.co.uk
2/7/2005 12:53:26 PM
[quoted text, click to view]

I found a solution as..

Dim XMLString As String = GetHtmlPageSource(URLString)
XMLString = XMLString.Replace("geo:lat", "latitude")
XMLString = XMLString.Replace("geo:long", "longitude")
XMLString = XMLString.Replace("icbm:latitude", "latitude")
XMLString = XMLString.Replace("icbm:longitude", "longitude")

Dim stream As StringReader
Dim reader As XmlTextReader

stream = New StringReader(XMLString)
reader = New XmlTextReader(stream)

Function GetHtmlPageSource(ByVal url As String, Optional ByVal username As _
String = Nothing, Optional ByVal password As String = Nothing) As String
Dim st As System.IO.Stream
Dim sr As System.IO.StreamReader

Try
' make a Web request
Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
' if the username/password are specified, use these credentials
If Not username Is Nothing AndAlso Not password Is Nothing Then
req.Credentials = New System.Net.NetworkCredential(username, _
password)
End If
' get the response and read from the result stream
Dim resp As System.Net.WebResponse = req.GetResponse
st = resp.GetResponseStream
sr = New System.IO.StreamReader(st)
' read all the text in it
Return sr.ReadToEnd
Catch ex As Exception
Return ""
Finally
' always close readers and streams
sr.Close()
st.Close()
End Try
End Function

AddThis Social Bookmark Button