11

I am building an website. Now I want to create its xml site map like google site map. But I want to create it programmatically using C#.

Can anybody tell me how I can access the root directory on the web server using base url of my website get all the pages list into a string list?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Soumita Parui
  • 143
  • 1
  • 2
  • 7
  • 1
    Did you try to use Directory.GetFiles()? – Amiram Korach Nov 19 '12 at 07:12
  • You must show some effort, some work that you have do. There are the functions that find the files, and there are also class that make xml file. And there are also examples on the internet. So start to make it and after a week of development come back if you have some small issues. – Aristos Nov 19 '12 at 07:15
  • I have tried a lot. bt nt able to retrive pages from server – Soumita Parui Nov 19 '12 at 07:20
  • check this: http://www.codeproject.com/Articles/18508/Simple-Sitemaps-in-ASP-NET http://www.codeproject.com/Articles/16860/Building-a-dynamic-SiteMap-in-ASP-NET-2-0-for-a-la http://stackoverflow.com/questions/3469140/web-sitemap-generation – Aristos Nov 19 '12 at 07:44

4 Answers4

14

come easy

private void GenerateXML()
    {
        try
        {
            string fileName         = "sitemap.xml";

            string DOMAIN           = "http://www.sohel-elite.com";
            string LAST_MODIFY= String.Format("{0:yyyy-MM-dd}", DateTime.Now);
            string CHANGE_FREQ      = "monthly";
            string TOP_PRIORITY     = "0.5";
            string MEDIUM_PRIORITY  = "0.8";

            XNamespace ns    = "http://www.sitemaps.org/schemas/sitemap/0.9";
            XNamespace xsiNs = "http://www.w3.org/2001/XMLSchema-instance";

            //XDocument Start
            XDocument xDoc = new XDocument(
                new XDeclaration("1.0", "UTF-8", "no"),
                new XElement(ns + "urlset",
                new XAttribute(XNamespace.Xmlns + "xsi", xsiNs),
                new XAttribute(xsiNs + "schemaLocation",
                    "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
                new XElement(ns + "url",

                    //Root Element
                    new XElement(ns + "loc",        DOMAIN),
                    new XElement(ns + "lastmod",    LAST_MODIFY),
                    new XElement(ns + "changefreq", "weekly"),
                    new XElement(ns + "priority",   TOP_PRIORITY)),

                    //Level0 Menu
                    from level0 in GetParentCMSMenu()
                        select new XElement(ns + "url",
                            new XElement(ns + "loc", String.Concat(DOMAIN, WebsiteHelpers.GetMenuRouteURL(Util.Parse<string>(level0.MENU_ALLIAS), Util.Parse<string>((level0.Level1 == null) ? string.Empty : level0.Level1), Util.Parse<int>(level0.APPLICATION_ID)))),
                            new XElement(ns + "lastmod",    LAST_MODIFY),
                            new XElement(ns + "changefreq", CHANGE_FREQ),
                            new XElement(ns + "priority",   MEDIUM_PRIORITY)
                        ),

                    //Level1 Menu
                    from level0 in GetParentCMSMenu()
                       from level1 in GetLevel1Menu(Util.Parse<int>(level0.MENU_ID))
                            select new XElement(ns + "url",
                                new XElement(ns + "loc", String.Concat(DOMAIN, WebsiteHelpers.GetMenuRouteURL(Util.Parse<string>(level1.Level1), Util.Parse<string>((level1.MENU_ALLIAS == null) ? string.Empty : level1.MENU_ALLIAS), Util.Parse<int>(level1.APPLICATION_ID)))),
                                new XElement(ns + "lastmod",    LAST_MODIFY),
                                new XElement(ns + "changefreq", CHANGE_FREQ),
                                new XElement(ns + "priority",   MEDIUM_PRIORITY)
                            ),

                    //Level2 Menu
                    from level0 in GetParentCMSMenu()
                        from level1 in GetLevel1Menu(Util.Parse<int>(level0.MENU_ID))
                            from level2 in GetLevel2Menu(Util.Parse<int>(level1.MENU_ID))
                                select new
                                    XElement(ns + "url",
                                    new XElement(ns + "loc", String.Concat(DOMAIN, WebsiteHelpers.GetMenuRouteURL(Util.Parse<string>(level2.Menu), Util.Parse<string>(level2.Level1), Util.Parse<int>(level2.AppID), Util.Parse<string>(level2.Level2)))),
                                    new XElement(ns + "lastmod", LAST_MODIFY),
                                    new XElement(ns + "changefreq", CHANGE_FREQ),
                                    new XElement(ns + "priority", MEDIUM_PRIORITY)
                                )

            ));
            //XDocument End

            xDoc.Save(Server.MapPath("~/") + fileName);

            this.MessageHolder.Visible = true;
            this.MessageHolder.Attributes.Add("class", "success");
            this.MessageHolder.InnerHtml = "Sitemap.xml created successfully";

        }
        catch (Exception ex)
        {
            this.MessageHolder.Visible = true;
            this.MessageHolder.Attributes.Add("class", "error");
            this.MessageHolder.InnerHtml = Constants.ERROR_LONG_MESSAGE + "<br/>" + ex.ToString();
        }
    }

is an excerpt from page :) Page

tahsin ilhan
  • 208
  • 2
  • 12
  • 1
    Can you edit your answer to contain more than just a link? Link-only answers are generally frowned upon on Stack Overflow, since the link could go dead at some point in the future, leaving your answer worthless. – Daniel Mann Feb 10 '14 at 14:19
  • 1
    Whoa.. XML Hell.. Remember DLL Hell? Painful to even to look at this... but what can we do? – Piotr Kula Jun 08 '15 at 14:16
10

I have made this library which makes it quite easy to create google sitemaps from a class or list a of urls.

https://github.com/aseemgautam/google-sitemap

A G
  • 21,087
  • 11
  • 87
  • 112
  • Your library is quite good. But i want to update sitemap automatically whenever a new page is created. How can we do that? – It's a trap Sep 19 '16 at 12:08
  • You can create a command line utility and integrate it in your build/deployment process – A G Sep 19 '16 at 12:46
  • wow not an a attribution to the original author... http://blog.mikecouturier.com/2011/07/seo-tip-dynamic-google-xml-sitemaps.html – The_Butcher Mar 06 '17 at 18:34
  • @The_Butcher my library is an extended version with some new features. You can compare and check. – A G Mar 07 '17 at 09:25
  • @AseemGautam Hi I tried this library but not able to generate the sitemap by providing Url. It is generating blank file. My code is as per below Sitemap sitemap = new Sitemap(); sitemap.Add(new SitemapLocation { ChangeFrequency = SitemapLocation.eChangeFrequency.monthly, Url = "https://modernpathshala.com", }); sitemap.WriteSitemapToFile(@"d://sitemap.xml"); – Ashish Shukla Jun 03 '17 at 15:24
4

You can have a look at this NuGet package (that support .Net and .Net Core) https://www.nuget.org/packages/xsitemap/

class Program
{
    static void Main(string[] args)
    {
        var sitemap = new Sitemap();

        sitemap.Add(new Url
        {
            ChangeFrequency = ChangeFrequency.Daily,
            Location = "http://www.example.com",
            Priority = 0.5,
            TimeStamp = DateTime.Now
        });

        sitemap.Add(CreateUrl("http://www.example.com/link1"));
        sitemap.Add(CreateUrl("http://www.example.com/link2"));
        sitemap.Add(CreateUrl("http://www.example.com/link3"));
        sitemap.Add(CreateUrl("http://www.example.com/link4"));
        sitemap.Add(CreateUrl("http://www.example.com/link5"));


        //Save sitemap structure to file
        sitemap.Save(@"d:\www\example.com\sitemap.xml");

        //Split a large list into pieces and store in a directory
        sitemap.SaveToDirectory(@"d:\www\example.com\sitemaps");

        //Get xml-content of file
        Console.Write(sitemap.ToXml());

        Console.ReadKey();
    }

    private static Url CreateUrl(string url)
    {
        return new Url
        {
            ChangeFrequency = ChangeFrequency.Daily,
            Location = url,
            Priority = 0.5,
            TimeStamp = DateTime.Now
        };
    }
}

The original project is available here https://github.com/ernado-x/X.Web.Sitemap

Et voilà ! :)

rdhainaut
  • 2,659
  • 1
  • 24
  • 34
1

If your your site pages are linked to each other and an orginary user can surf all of them (having necessary links in pages content), it is possible to create a list of site's webpages recursively and put it to an xml file (adhering to standards of sitemap protocol) Code snippet of url list generator from working app:

...
   new_urls.Add(BaseUrl);  //first url
   do
   {
      List hrefs=new List();
      foreach (var url in new_urls)
      {
         string text =await _loader.Get(url);
         if (string.IsNullOrEmpty(text)) continue;

         visited.Add(url);
         List meta=Parser.GetAHrefs(text).Distinct().ToList();  //getting list of links
         Parser.Normalize(Domain,url,ref meta);
         if (Exclude)  //option to exclude query from url
             meta = meta.Select(u => u.Contains('?') ? u.Split('?')[0] : u).ToList();
         hrefs.AddRange(meta);
         hrefs = hrefs.Distinct().ToList();
       }
       new_urls = hrefs.Except(visited).ToList();   //excluding visited pages
    }
    while (new_urls.Count != 0);
...

Plain text to xml parsing method:

public void Save(string path)
        {
            string doc = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?>";

            doc += OpenTag("urlset", "xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"");

            if (UseOpt)
            {
                foreach (var url in Urls)
                {
                    doc += OpenTag("url");
                    doc += Tag("loc", url);
                    doc += Tag("lastmod", LastMode);
                    doc += Tag("changefreq", Changefreq);
                    doc += Tag("priority", Priority);
                    doc += CloseTag("url");
                }
            }
            else
            {
                foreach(var url in Urls)
                {
                    doc += OpenTag("url");
                    doc += Tag("loc", url);
                    doc += CloseTag("url");
                }
            }

            doc += CloseTag("urlset");

            File.WriteAllText(path,doc);
        }