Generate google sitemap xml
Posted 0banana0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Generate google sitemap xml相关的知识,希望对你有一定的参考价值。
方式一、
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的主要内容,如果未能解决你的问题,请参考以下文章