Google sitemap xml LINQpad

Here's a query you can run directly in LINQPad to create a google sitemap

Select language > C# Statement(s) on the LINQpad menu, connect your database and specify the table/fields in the code below


            var styleInstruction = new XProcessingInstruction(
                "xml-stylesheet", "href='styles.css' type='text/css'"

        XNamespace Xname = "";

        var docType = new XDocumentType("html",
          "-//W3C//DTD XHTML 1.0 Strict//EN",
          "", null);

        var categories =
          new XElement(Xname + "urlset",
              from c in Categories // specify your table name
                        new XElement("url",
                            new XElement("loc", "" + c.CategoryID + "-" + c.CategoryName), // specify your display fields
                            new XElement("lastmod", c.DateCreated.ToString("yyyy-MM-dd")) // this date format is vital 
                            new XElement("changefreq", "monthly"),
                            new XElement("priority", 0.8)

        var doc =
            new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),

        string tempPath = Path.Combine(Path.GetTempPath(), "sample.xml");
        doc.Save (tempPath);
        // This will display the page in IE or FireFox
        Process.Start (tempPath);
        // this will display contents of the xml file we just created in the pane below 
        File.ReadAllText (tempPath).Dump();