Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
_
Public Class rssClass
Private _channel As channelClass
_
Public Property channel() As channelClass
Get
Return _channel
End Get
Set(ByVal value As channelClass)
_channel = value
End Set
End Property
End Class
End Class
在该对象中包含chanelClass,保持和xml的文件相同的结构
接下来我们需要定义chanelClass,代码如下:
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
Partial Class rssClass
Partial Class channelClass
Private _title As String
'''
''' The title of the feed, which includes the location city. For example "Yahoo! Weather - Sunnyvale, CA"
'''
_
Public Property title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Private _link As String
'''
''' The URL for the Yahoo! Weather page of the forecast for this location. For example http://us.rd.yahoo.com/dailynews/rss/weather/ Sunnyvale__CA/ *http://xml.weather.yahoo.com/ forecast/94089_f.html
'''
_
Public Property link() As String
Get
Return _link
End Get
Set(ByVal value As String)
_link = value
End Set
End Property
Private _language As String
'''
''' The language of the weather forecast, for example, en-us for US English.
'''
_
Public Property language() As String
Get
Return _language
End Get
Set(ByVal value As String)
_language = value
End Set
End Property
Private _description As String
'''
''' The overall description of the feed including the location, for example "Yahoo! Weather for Sunnyvale, CA"
'''
_
Public Property description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Private _lastBuildDate As String
'''
''' The last time the feed was updated. The format is in the date format defined by RFC822 Section 5, for example Mon, 256 Sep 17:25:18 -0700.
'''
_
Public Property lastBuildDate() As String
Get
Return _lastBuildDate
End Get
Set(ByVal value As String)
_lastBuildDate = value
End Set
End Property
Private _ttl As String
'''
''' Time to Live; how long in minutes this feed should be cached.
'''
_
Public Property ttl() As String
Get
Return _ttl
End Get
Set(ByVal value As String)
_ttl = value
End Set
End Property
Private _location As locationClass
'''
''' The location of this forecast.
'''
_
Public Property location() As locationClass
Get
Return _location
End Get
Set(ByVal value As locationClass)
_location = value
End Set
End Property
Private _units As unitsClass
'''
''' Units for various aspects of the forecast.
'''
_
Public Property units() As unitsClass
Get
Return _units
End Get
Set(ByVal value As unitsClass)
_units = value
End Set
End Property
Private _wind As windClass
'''
''' Forecast information about wind.
'''
_
Public Property wind() As windClass
Get
Return _wind
End Get
Set(ByVal value As windClass)
_wind = value
End Set
End Property
Private _atmosphere As atmosphereClass
'''
''' Forecast information about current atmospheric pressure, humidity, and visibility.
'''
_
Public Property atmosphere() As atmosphereClass
Get
Return _atmosphere
End Get
Set(ByVal value As atmosphereClass)
_atmosphere = value
End Set
End Property
Private _astronomy As astronomyClass
'''
''' Forecast information about current astronomical conditions.
'''
_
Public Property astronomy() As astronomyClass
Get
Return _astronomy
End Get
Set(ByVal value As astronomyClass)
_astronomy = value
End Set
End Property
Private _image As imageClass
'''
''' The image used to identify this feed
'''
_
Public Property image() As imageClass
Get
Return _image
End Get
Set(ByVal value As imageClass)
_image = value
End Set
End Property
Private _item As itemClass
'''
''' The local weather conditions and forecast for a specific location.
'''
_
Public Property item() As itemClass
Get
Return _item
End Get
Set(ByVal value As itemClass)
_item = value
End Set
End Property
End Class
End Class
End Class
在chanel中声明了他包含哪些子节点,同样我们也需要为这些子节点申明对象。
Imports Microsoft.VisualBasic
Imports System.Xml.Serialization
Partial Class YahooWeatherForecast
Partial Class rssClass
Partial Class channelClass
Public Class atmosphereClass
Private _humidity As Integer
'''
''' humidity, in percent
'''
_
Public Property humidity() As Integer
Get
Return _humidity
End Get
Set(ByVal value As Integer)
_humidity = value
End Set
End Property
Private _visibility As Double
'''
''' in the units specified by the distance attribute of the units element (mi or km). Note that the visibility is specified as the actual value * 100. For example, a visibility of 16.5 miles will be specified as 1650. A visibility of 14 kilometers will appear as 1400.
'''
_
Public Property visibility() As Double
Get
Return _visibility
End Get
Set(ByVal value As Double)
_visibility = value
End Set
End Property
Private _pressure As Double
'''
''' barometric pressure, in the units specified by the pressure attribute of the units element (in or mb).
'''
_
Public Property pressure() As Double
Get
Return _pressure
End Get
Set(ByVal value As Double)
_pressure = value
End Set
End Property
Private _rising As Integer
'''
''' state of the barometric pressure, steady = 0, rising = 1, falling = 2
'''
_
Public Property rising() As Integer
Get
Return _rising
End Get
Set(ByVal value As Integer)
_rising = value
End Set
End Property
End Class
End Class
End Class
End Class
在申明完对象之后,我们就可以将xml文件转换为相应的对象。
代码如下:
Dim doc As New System.Xml.XmlDocument()
Dim str As String = "http://xml.weather.yahoo.com/forecastrss?p=" & p & "&u=" & U
Dim c As rssClass
'Load data
doc.Load(str)
str = doc.OuterXml
'XmlSerializer could not Serialize XML with this Elements so i just replace them with Null String!
str = str.Replace("yweather:", "")
str = str.Replace("geo:", "")
'Dim str1 As String = """ : "
Dim str1 = "isPermaLink=""false"""
str = str.Replace(str1, "")
str1 = " version=""2.0"" xmlns:yweather=""http://xml.weather.yahoo.com/ns/rss/1.0"" xmlns:geo=""http://www.w3.org/2003/01/geo/wgs84_pos#"""
str = str.Replace(str1, "")
Dim memoryStream As New IO.MemoryStream()
Dim d As New IO.StringReader(str)
Dim xs As New XmlSerializer(GetType(rssClass))
c = CType(xs.Deserialize(d), rssClass)
rss = c
最后用户可以根据自己的需要将对象中的属性取出,使用于相关的应用中。类似如下代码
Dim t As New YahooWeatherLib.YahooWeatherForecast("CHXX0044", "f")
Label1.Text = t.rss.channel.item.forecast.high
Label2.Text = t.rss.channel.item.forecast.low
Label5.Text = t.rss.channel.item.title