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