1. 程式人生 > >Generate google sitemap xml

Generate google sitemap xml

video quest lan microsoft concepts eat lse nth .com

方式一、

 XNamespace xNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9";
            XNamespace xhtml = "http://www.w3.org/1999/xhtml";
            XElement root = new XElement(
                xNamespace + "urlset",
                new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
                
new XAttribute(XNamespace.Xmlns + "xhtml", "http://www.w3.org/1999/xhtml"), new XElement( xNamespace.GetName("url"),// + "url" new XElement(xNamespace.GetName("loc"), "http://www.example.com/english/page.html"), new XElement(xhtml + "
link", new XAttribute("rel", "alternate"), new XAttribute("hreflang", "de"), new XAttribute("href", "http://www.example.com/deutsch/page.html")) ) ); Console.WriteLine(root);

方式二、

XNamespace xNamespace = "http://www.sitemaps.org/schemas/sitemap/0.9";
            XNamespace xhtml = "http://www.w3.org/1999/xhtml";


            XElement root = new XElement(xNamespace + "urlset");
            root.SetAttributeValue("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
            root.SetAttributeValue(XNamespace.Xmlns + "xhtml", "http://www.w3.org/1999/xhtml");


            ////創建子節點
            XElement url = new XElement(xNamespace.GetName("url"));
            url.SetElementValue(xNamespace.GetName("loc"), "https://translate.google.cn");
            url.SetElementValue(xNamespace.GetName("lastmod"), "2018-10-22");
            url.SetElementValue(xNamespace.GetName("changefreq"), "monthly");
            url.SetElementValue(xNamespace.GetName("priority"), "0.9");

            //xhtml:link 
            XElement twlink = new XElement(xhtml + "link");
            twlink.SetAttributeValue("rel", "alternate");
            twlink.SetAttributeValue("hreflang", "zh-tw");
            twlink.SetAttributeValue("href", "https://translate.google.cn");
            url.Add(twlink);

            XElement uslink = new XElement(xhtml + "link");
            uslink.SetAttributeValue("rel", "alternate");
            uslink.SetAttributeValue("hreflang", "en-tw");
            uslink.SetAttributeValue("href", "https://translate.google.cn");
            url.Add(uslink);
            //添加節點到父節點,添加根節點到對象
            root.Add(url);
            Console.WriteLine(root);

控制臺打印效果:(生成文件也是一樣的)

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>http://www.example.com/english/page.html</loc>
    <xhtml:link rel="alternate" hreflang="de" href="http://www.example.com/deutsch/page.html" />
  </url>
</urlset>
-------------------------
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url>
    <loc>https://translate.google.cn</loc>
    <lastmod>2018-10-22</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.9</priority>
    <xhtml:link rel="alternate" hreflang="zh-tw" href="https://translate.google.cn" />
    <xhtml:link rel="alternate" hreflang="en-tw" href="https://translate.google.cn" />
  </url>
</urlset>

參考資料:


https://support.google.com/webmasters/answer/75712?hl=zh-Hant&ref_topic=4581190
https://support.google.com/webmasters/answer/189077?hl=zh-Hant&ref_topic=2370587&visit_id=636757708970115411-988286469&rd=1
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/how-to-create-a-document-with-namespaces-linq-to-xml
https://stackoverflow.com/questions/34671195/how-to-format-xml-using-linq
https://stackoverflow.com/questions/23223014/video-sitemap-colon-in-name-using-linq-to-xml

Generate google sitemap xml