by
Neon Quach
23. January 2010 18:35
Problem
You want to get url link from sitemap.xml file.
Solution:
After downloading sitemap.xml string from internet by using WebClient, you can easy get url link form it by using Linq2Xml.
Here is the snippets code both C# and VB.NET
For demo purpose I use Console application.
C#
using System;
using System.Linq;
using System.Net;
using System.Xml.Linq;
namespace CS
{
class Program
{
static void Main(string[] args)
{
WebClient client = new WebClient();
string sitemap = client.DownloadString("http://code2code.info/sitemap.xml");
XElement element = XElement.Parse(sitemap);
var e = from i in element.Elements()
select i.FirstNode;
foreach (XNode item in e)
{
string url = ((XElement)(item)).Value;
Console.WriteLine(url);
}
Console.Read();
}
}
}
VB.NET
Imports System.Net
Imports System.Linq
Module Module1
Sub Main()
Dim client As New WebClient
Dim str As String = client.DownloadString("http://code2code.info/sitemap.xml")
Dim element As XElement = XElement.Parse(str)
Dim e = From i In element.Elements() _
Select i.FirstNode
For Each item As XNode In e
Dim url As String = (DirectCast(item, XElement)).Value
Console.WriteLine(url)
Next
Console.Read()
End Sub
End Module
When you run this code, you will see the result like screenshot below

Don’t forget checkout my code at: http://code2code.googlecode.com
GetUrlFromSitemap.rar (62.55 kb)