Reputation: 3
I have been trying to open the following XML file in VB.NET using the Linq library.
<?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>
The code that I'm using works with normal XML files, but whenever I add the xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
attribute to the root node, nothing is getting returned by the application. Here is the VB.NET code that is reading the XML file:
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
Upvotes: 0
Views: 1358
Reputation: 56162
That is because sitemap.xml
has default namespace http://www.sitemaps.org/schemas/sitemap/0.9
. You should define XNamespace
, then use it in queries, i.e.:
C# code:
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
foreach (var element in XMLFile.Descendants(ns + "url"))
{
...
}
Upvotes: 1