[英]How to read a sitemap using VB.NET
我一直在嘗試使用Linq庫在VB.NET中打開以下XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://wegotflash.com/sitemap.xsl"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://wegotflash.com</loc>
<lastmod>2012-02-19</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
<url>
<loc>http://wegotflash.com/cat/1/shooter/newest-1</loc>
<lastmod>2012-02-19</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
我使用的代碼適用於常規XML文件,但是每當我將xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
屬性添加到根節點時,應用程序都不會返回任何內容。 這是讀取XML文件的VB.NET代碼:
Dim XMLFile As XDocument = XDocument.Load(TextBox1.Text)
For Each url As XElement In XMLFile.Descendants("url")
If url.HasElements Then
MessageBox.Show(url.Element("loc").Value)
End If
Next
這是因為sitemap.xml
具有默認名稱空間http://www.sitemaps.org/schemas/sitemap/0.9
。 您應該定義XNamespace
,然后在查詢中使用它,即:
C#代碼:
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
foreach (var element in XMLFile.Descendants(ns + "url"))
{
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.