0

所以基本上,我手头有这个 RSS 提要。我通过以下方式将 RSS 转换为 XmlDocument:

 Public Function GroveHallFromRss() As String
        Dim webClient As System.Net.WebClient = New System.Net.WebClient()
        Dim ourUrl As String = "http://abc.123.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 root As XmlNode = xmlDocument.DocumentElement
    Return root.OuterXml.ToString

    End Function

有了上面,我可以得到 RSS 的内容,但它以标签开头,以<string>标签结尾</string>。中间的内容都是压缩在一起的纯文本。

如果我删除 OuterXml 部分,只是Return root.ToString,我得到以下内容:

此 XML 文件似乎没有任何与之关联的样式信息。文档树如下所示。 <string>System.Xml.XmlElement</string>

所以我想知道如何将转换后的 RSS 显示为任何其他带有节点样式的 XML 文件。(但不是由浏览器呈现和显示为 RSS,而只是样式化的 XML)

4

2 回答 2

1

首先,没有必要在您当前的代码中将 XML 加载到 XmlDocument 对象中。例如,下面的代码做同样的事情:

Public Function GroveHallFromRss() As String
    Dim webClient As System.Net.WebClient = New System.Net.WebClient()
    Dim ourUrl As String = "http://abc.123.org/RSSSyndicator.aspx?type=N&range=currentyear&expire=Y&location=2-7-165&rssid=18"
    Dim streamReader As StreamReader = New StreamReader(webClient.OpenRead(ourUrl))
    Return streamReader.ReadToEnd()
End Function

但是,这并不能回答您关于正确缩进文档的问题。为此,我相信最简单的方法是应用样式表。以下 XSLT 脚本将使 XML 文档变得漂亮:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:template match="/">
    <xsl:copy-of select="."/>
  </xsl:template>
</xsl:stylesheet>

因此,您可以在代码中应用 XSLT 脚本:

Public Function GroveHallFromRss() As String
    Dim webClient As System.Net.WebClient = New System.Net.WebClient()
    Dim ourUrl As String = "http://abc.123.org/RSSSyndicator.aspx?type=N&range=currentyear&expire=Y&location=2-7-165&rssid=18"
    Dim reader as XmlTextReader = new XmlTextReader(webClient.OpenRead(ourUrl))
    Dim transformer As XslCompiledTransform = New XslCompiledTransform()
    transformer.Load("<?xml version=""1.0"" encoding=""utf-8""?><xsl:stylesheet version=""1.0"" xmlns:xsl=""http://www.w3.org/1999/XSL/Transform""><xsl:output method=""xml"" indent=""yes""/><xsl:strip-space elements=""*""/><xsl:template match=""/""><xsl:copy-of select="".""/></xsl:template></xsl:stylesheet>")
    Dim settings As XmlWriterSettings = transformer.OutputSettings
    Dim outputStream As MemoryStream = New MemoryStream()
    Dim xmlWriter As XmlWriter = xmlWriter.Create(outputStream, settings)
    transformer.Transform(reader, Nothing, xmlWriter)
    outputStream.Position = 0
    Dim streamReader As StreamReader = New StreamReader(outputStream)
    Return = streamReader.ReadToEnd()
End Function
于 2012-06-08T18:17:56.623 回答
0

Thanks, actually my friend helped me with a small change. That is changing As String to As XmlDocument. that did it. so "Public Function GroveHallFromRss() As XmlDocument" Thanks for you help though.

于 2012-06-08T18:55:03.777 回答