Generate Google sitemap xml

Source: Internet
Author: User

Way One,

XNamespace XNamespace ="http://www.sitemaps.org/schemas/sitemap/0.9"; XNamespace XHTML="http://www.w3.org/1999/xhtml"; XElement Root=NewXElement (XNamespace+"Urlset",                NewXAttribute ("xmlns","http://www.sitemaps.org/schemas/sitemap/0.9"),                NewXAttribute (Xnamespace.xmlns +"XHTML","http://www.w3.org/1999/xhtml"),                    NewXElement (Xnamespace.getname ("URL"),//+ "url"                        NewXElement (Xnamespace.getname ("Loc"),"http://www.example.com/english/page.html"),                        NewXElement (XHTML +"Link",                            NewXAttribute ("rel","Alternate"),                            NewXAttribute ("Hreflang","de"),                            NewXAttribute ("href","http://www.example.com/deutsch/page.html"))                    )                ); Console.WriteLine (root);

Way Two,

XNamespace XNamespace ="http://www.sitemaps.org/schemas/sitemap/0.9"; XNamespace XHTML="http://www.w3.org/1999/xhtml"; XElement Root=NewXElement (XNamespace +"Urlset"); Root. Setattributevalue ("xmlns","http://www.sitemaps.org/schemas/sitemap/0.9"); Root. Setattributevalue (Xnamespace.xmlns+"XHTML","http://www.w3.org/1999/xhtml"); ////Create child nodesXElement URL =NewXElement (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:linkXElement Twlink =NewXElement (XHTML +"Link"); Twlink. Setattributevalue ("rel","Alternate"); Twlink. Setattributevalue ("Hreflang","ZH-TW"); Twlink. Setattributevalue ("href","https://translate.google.cn"); Url.            ADD (Twlink); XElement Uslink=NewXElement (XHTML +"Link"); Uslink. Setattributevalue ("rel","Alternate"); Uslink. Setattributevalue ("Hreflang","EN-TW"); Uslink. Setattributevalue ("href","https://translate.google.cn"); Url.            ADD (Uslink); //Add a node to the parent node, add the root node to the objectRoot.            ADD (URL); Console.WriteLine (root);

Console Print Effect: (the same is the generation file)

<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-Ten- A</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>

Resources:


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.