项目作者: xin9le

项目描述 :
Simple sitemap.xml serializer for better SEO.
高级语言: C#
项目地址: git://github.com/xin9le/XSitemaps.git
创建时间: 2020-01-11T08:06:16Z
项目社区:https://github.com/xin9le/XSitemaps

开源协议:MIT License

下载


XSitemaps

SEO (= Search Engine Optimization) is very important to improve the page views of your website. Sitemaps are defined in a simple XML-formatted file that can be read by search engines to more accurately crawl your site. And also Sitemaps are widely supported by many companies, including Google, Yahoo!, and Microsoft. See sitemaps.org for more details.

This library provides a simple and easy to use sitemap.xml serializer.

Releases

Supported features

  • Sitemap file serialization
  • SitemapIndex file serialization
  • Split files according to the number of URLs
  • Controllable indent
  • GZIP compression

Support platform

  • .NET Standard 1.1+

Create Sitemap.xml

  1. //--- Create Sitemaps
  2. var modifiedAt = DateTimeOffset.Now;
  3. var urls = new[]
  4. {
  5. new SitemapUrl("https://blog.xin9le.net", modifiedAt, ChangeFrequency.Daily, priority: 1.0),
  6. new SitemapUrl("https://blog.xin9le.net/entry/rx-intro"),
  7. new SitemapUrl("https://blog.xin9le.net/entry/async-method-intro", frequency: ChangeFrequency.Weekly),
  8. };
  9. var sitemaps = Sitemap.Create(urls, maxUrlCount: 2);
  10. //--- Output to files
  11. for (var i = 0; i < sitemaps.Length; i++)
  12. {
  13. var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  14. var path = Path.Combine(desktop, $"Sitemap_{i}.xml");
  15. using (var stream = new FileStream(path, FileMode.CreateNew))
  16. {
  17. var options = new SerializeOptions
  18. {
  19. EnableIndent = true,
  20. EnableGzipCompression = false,
  21. };
  22. sitemaps[i].Serialize(stream, options);
  23. }
  24. }
  25. //--- Sitemap_0.xml
  26. /*
  27. <?xml version="1.0" encoding="utf-8"?>
  28. <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  29. <url>
  30. <loc>https://blog.xin9le.net</loc>
  31. <lastmod>2020-01-12T00:07:12.2351485+09:00</lastmod>
  32. <changefreq>daily</changefreq>
  33. <priority>1</priority>
  34. </url>
  35. <url>
  36. <loc>https://blog.xin9le.net/entry/rx-intro</loc>
  37. <changefreq>never</changefreq>
  38. <priority>0.5</priority>
  39. </url>
  40. </urlset>
  41. */
  42. //--- Sitemap_1.xml
  43. /*
  44. <?xml version="1.0" encoding="utf-8"?>
  45. <urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  46. <url>
  47. <loc>https://blog.xin9le.net/entry/async-method-intro</loc>
  48. <changefreq>weekly</changefreq>
  49. <priority>0.5</priority>
  50. </url>
  51. </urlset>
  52. */

Create SitemapIndex.xml

  1. //--- Create SitemapIndex
  2. var modifiedAt = DateTimeOffset.Now;
  3. var info = new[]
  4. {
  5. new SitemapInfo("https://example.com/Sitemap_0.xml", modifiedAt),
  6. new SitemapInfo("https://example.com/Sitemap_1.xml"),
  7. };
  8. var index = new SitemapIndex(info);
  9. //--- Output to file
  10. var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  11. var path = Path.Combine(desktop, $"SitemapIndex.xml");
  12. using (var stream = new FileStream(path, FileMode.CreateNew))
  13. {
  14. var options = new SerializeOptions
  15. {
  16. EnableIndent = true,
  17. EnableGzipCompression = false,
  18. };
  19. index.Serialize(stream, options);
  20. }
  21. /*
  22. <?xml version="1.0" encoding="utf-8"?>
  23. <sitemapindex xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  24. <sitemap>
  25. <loc>https://example.com/Sitemap_0.xml</loc>
  26. <lastmod>2020-01-12T00:13:24.4802279+09:00</lastmod>
  27. </sitemap>
  28. <sitemap>
  29. <loc>https://example.com/Sitemap_1.xml</loc>
  30. </sitemap>
  31. </sitemapindex>
  32. */

Installation

Getting started from downloading NuGet package.

  1. PM> Install-Package XSitemaps

License

This library is provided under MIT License.

Author

Takaaki Suzuki (a.k.a @xin9le) is software developer in Japan who awarded Microsoft MVP for Developer Technologies (C#) since July 2012.