Get Url from sitemap.xml using Linq2Xml

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)

Tags: , , , ,

C# | VB.NET

Comments

1/21/2010 9:51:55 PM #

trackback

Get Url from sitemap.xml using Linq2Xml

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout

2/5/2010 5:22:30 AM #

Conference Call System Suppliers

Yes! Were looking for this in VB.net. Thanks a lot, Neon!
Anyway, is there a way to extract it and save it into text file? Is StreamWriter doable?

Conference Call System Suppliers United States

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0 - Eco Theme by n3o Web Designers