简体   繁体   English

如何使用VB.NET读取站点地图

[英]How to read a sitemap using VB.NET

I have been trying to open the following XML file in VB.NET using the Linq library. 我一直在尝试使用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>

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. 我使用的代码适用于常规XML文件,但是每当我将xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"属性添加到根节点时,应用程序都不会返回任何内容。 Here is the VB.NET code that is reading the XML file: 这是读取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

That is because sitemap.xml has default namespace http://www.sitemaps.org/schemas/sitemap/0.9 . 这是因为sitemap.xml具有默认名称空间http://www.sitemaps.org/schemas/sitemap/0.9 You should define XNamespace , then use it in queries, ie: 您应该定义XNamespace ,然后在查询中使用它,即:

C# code: 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM