2012-02-21 9 views
0

私はLinqライブラリを使用してVB.NETで次のXMLファイルを開こうとしています。VB.NETを使用してサイトマップを読み取る方法

<?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"属性を追加するたびに、何もアプリケーションによって返さなかっなっています。

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 

答えて

1

sitemap.xmlは、デフォルトの名前空間http://www.sitemaps.org/schemas/sitemap/0.9を持っているためである。ここではXMLファイルを読んでいるVB.NETコードです。あなたは、その後、XNamespace定義クエリでそれを使用する必要がありますすなわち:

C#コード:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
foreach (var element in XMLFile.Descendants(ns + "url")) 
{ 
    ... 
} 
関連する問題