使用c#从URL创建xml站点地图

时间:2014-12-30 22:19:02

标签: c# xml sitemap

我正在尝试创建一个使用c#创建xml站点地图的实用程序。我能够将它指向本地文件夹并生成在浏览器中显示的结果,但是当我在网站上指出它时遇到了麻烦。任何提示或提示将不胜感激!我对c#很新。

    static void Main(string[] args)
    {   
        //String sDir = @"WWW.WEBSITE.COM";<---I'd like to index a site rather than a file.
        String sDir = @"FOLDER_LOCATION";
        String siteMap = @"FOLDER_LOCATION\sitemapworking.xml";//Where I'd like to save the sitemap
        var sb = new System.Text.StringBuilder();


        String[] allfiles = System.IO.Directory.GetFiles(sDir, "*", System.IO.SearchOption.AllDirectories);
        String[] alldirects = System.IO.Directory.GetDirectories(sDir, "*", System.IO.SearchOption.AllDirectories);

        sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset>");
        sb.AppendLine("<url>\n\t<loc>" + sDir + "</loc>\n</url>");

        foreach (string s in alldirects)
        {
            sb.AppendLine("<url>\n\t<loc>" + s + "</loc>\n</url>");
        }

        foreach (string s in allfiles)
        {
            System.IO.FileInfo file = new System.IO.FileInfo(s);
            sb.AppendLine("<url>\n\t<loc>" + s + "</loc>\n\t<lastmod> " + file.LastWriteTime + "</lastmod>\n</url>");
        }
        sb.AppendLine("</urlset>");
        File.WriteAllText(siteMap, sb.ToString());
    }

0 个答案:

没有答案