首先,没有必要在您当前的代码中将 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