我正在尝试构建一个 Web 服务,它应该从现有的 XML 中构建一个新的 XML。
代码如下:
<WebMethod(CacheDuration:=0, Description:="GroveHallFromRss")> _
Public Function GroveHallFromRss() As XmlDocument
Dim webClient As System.Net.WebClient = New System.Net.WebClient()
Dim ourUrl As String = "http://123.example.org/RSSSyndicator.aspx?type=N&range=currentyear&expire=Y&location=2-7-165&rssid=18"
Dim stream AS Stream
stream = webClient.OpenRead(ourUrl)
Dim xmlDocument AS XmlDocument = new XmlDocument()
xmlDocument.Load(stream)
Dim myXml As XmlDocument = new XmlDocument()
Using writer As XmlWriter = myXml.CreateNavigator().AppendChild()
writer.WriteStartDocument()
writer.WriteStartElement("document")
For Each item As System.Xml.XmlElement In xmlDocument.Item("item")
writer.WriteStartElement("event")
writer.WriteElementString("title", item.Item("title").Value)
writer.WriteElementString("link", item.Item("link").Value)
writer.WriteElementString("description", item.Item("description").Value)
writer.WriteEndElement()
Next
writer.WriteEndElement()
writer.WriteEndDocument()
End Using
Return myXml
它给了我这样的错误:
System.NullReferenceException:对象引用未设置为对象的实例。在 Portal.GroveHallFromRss()
问题似乎在以下范围内:
对于每个项目作为 System.Xml.XmlElement 在 xmlDocument.Item("item")
但我不确定是什么原因造成的。谢谢!
这是 XML:
<rss version="2.0">
-
<channel>
-
<title>
title 123
</title>
<cf:treatAs>list</cf:treatAs>
<link>http://123/default.aspx</link>
<description>RSS Feed 123 Events Calendar</description>
-
<item>
<title>New 123 (6/18/2012)</title>
-
<link>
http://123.aspx?view=EventDetails&eventidn=9751&information_id=19501&type=&rss=rss
</link>
-
<description>
<table cellpadding="5" cellspacing="0" border="0"><tr><td valign="top"><table cellpadding="0" cellspacing="0" border="0"><tr><td style="padding-bottom:1px;"><b>Start Date:</b> </td><td style="padding-bottom:1px;">6/18/2012</td><td> <b>Start Time:</b> </td><td>8:00 AM</td></tr><tr><td><b>End Date:</b> </td><td>6/18/2012</td><td> <b>End Time:</b> </td><td>4:00 PM</td></tr></table><br />123<br />Room: 123<br /><br />nil</td></tr></table>
</description>
<pubDate>Mon, 18 Jun 2012 12:00:00 GMT</pubDate>
<category>06/18/2012</category>
</item>
+
<item>
<title>123 Orientation (6/19/2012)</title>
-
<link>
http://123/EventList.aspx?view=EventDetails&eventidn=9770&information_id=19539&type=&rss=rss
</link>
-
<description>
<table cellpadding="5" cellspacing="0" border="0"><tr><td valign="top"><table cellpadding="0" cellspacing="0" border="0"><tr><td style="padding-bottom:1px;"><b>Start Date:</b> </td><td style="padding-bottom:1px;">6/19/2012</td><td> <b>Start Time:</b> </td><td>8:00 AM</td></tr><tr><td><b>End Date:</b> </td><td>6/19/2012</td><td> <b>End Time:</b> </td><td>4:00 PM</td></tr></table><br />123<br />Room: 106<br /><br />nil</td></tr></table>
</description>
<pubDate>Tue, 19 Jun 2012 12:00:00 GMT</pubDate>
<category>06/19/2012</category>
</item>
-
<item>
<title>123 (6/20/2012)</title>
-
<link>
http://123/EventList.aspx?view=EventDetails&eventidn=9789&information_id=19577&type=&rss=rss
</link>
-
<description>
<table cellpadding="5" cellspacing="0" border="0"><tr><td valign="top"><table cellpadding="0" cellspacing="0" border="0"><tr><td style="padding-bottom:1px;"><b>Start Date:</b> </td><td style="padding-bottom:1px;">6/20/2012</td><td> <b>Start Time:</b> </td><td>8:00 AM</td></tr><tr><td><b>End Date:</b> </td><td>6/20/2012</td><td> <b>End Time:</b> </td><td>4:00 PM</td></tr></table><br />123 Grove Hall<br />Room: 106<br /><br />nil</td></tr></table>
</description>
<pubDate>Wed, 20 Jun 2012 12:00:00 GMT</pubDate>
<category>06/20/2012</category>
</item>
</channel>
</rss>