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>--</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的更多相关文章
- 创建Google网站地图Sitemap.xml
Sitemap.xml是google搞出来的,也就是网站地图,不过这个网站地图是用xml写的,而且要按google的标准来写,并且要将写出来的这个文件sitemap.xml上传到自己的服务器空间中去. ...
- ecshop 改变sitemap.xml的位置
大家知道ECSHOP默认的sitemap.xml文件是放置在data文件夹中的,但是这不利于GOOGLE的抓取.我们必须把sitemap.xml文件放置在根目录下 在admin/sitemap.php ...
- ecshop优化修改sitemap.xml到根目录
大家都知道sitemap.xml是用来给搜索引擎提交收录的工具,虽然搜索引擎自己也会收录网站但是有了sitemap.xml之后速度会加快不少.而ecshop程序是有自动生成sitemap.xml的功能 ...
- sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml
Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间.更改的 ...
- google sitemap
引言 刚开始以为要一个绿色快速通道网页,涵盖常用的地址链接,以便于google的爬虫统计数据,然后看了google sitemap站点文档,原来站点地图是一种文件,您可以通过该文件列出您网站上的网页, ...
- sitemap xml文件生成
sitemap xml生成方法 <?php /** * SitemapService.php. * * 生成sitemap */ class Sitemap { public $newLine ...
- 帝国CMS如何自动生成sitemap.xml网站地图文件
登录网站的后台http://你的域名/e/admin/ 进入后台栏目 =>增加自定义页面 =>选择直接页面,页面名称为:网站地图,文件名修改为 ../../sitemap.xml 内容填 ...
- 多语言的sitemap xml
请注意一下 sitemap xml 也有多语言的
- php生成百度站点地图sitemap.xml
<?php header("Content-type:text/html;charset=utf-8"); //php生成百度站点地图sitemap.xml //http:/ ...
随机推荐
- Winform-DataGridView
Winform-DataGridView 1 常用属性 // 1.点击后的选中模式 this.dgv.SelectionMode = DataGridViewSelectionMode.FullRow ...
- selenium 安装 以及相关环境
在cmd中安装简单, pip install selenium 一键安装 如果需要 chromedriver 还需要安装相对应的 版本 看到网上基本没有最新的chromedriver与chr ...
- SpringBoot启动banner更改
这篇文章的开始先给大家看一个图片 用过或者看过springboot的人都知道,这就是springboot启动的banner,这一篇介绍如何自定义springboot的启动bannner. 先介绍一个可 ...
- Java性能调优zz
写Java也有n年了,现在还是有不少的坏的代码习惯,也通过学习别人的代码学到了不少好的习惯.这篇文章主要是整理的资料.留给自己做个警戒,提示以后写代码的时候注意!在文章的后面,会提供整理的原材料下载. ...
- eclipse 导入android 项目重名解决方法
在进行andoid 开发时会用到许多的开源框架,但有时候发现几天框架的项目名字都是 library. 解决方法: 1.如果项目中没有 .project 文件,直接修改项目文件夹的名字即可,注意在你引用 ...
- Dijkstra求次短路
#10076.「一本通 3.2 练习 2」Roadblocks:https://loj.ac/problem/10076 解法: 次短路具有一种性质:次短路一定是由起点到点x的最短路 + x到y的距离 ...
- unity游戏设计与实现 --读书笔记(一)
1, 游戏入门的一些知识点,游戏对象GameObject(角色), 组件Compoent(角色的功能),资源Asset(美术素材呵呵音频等的数据),场景Scene(用以放置各个角色,负责展示画面), ...
- redis:order set有序集合类型的操作(有序集合)
1. order set有序集合类型的操作(有序集合) 有序集合是在无序集合的基础上加了一个排序的依据,这个排序依据叫score,因此声明一个集合为有序集合的时候要加上score(作为排序的依据) 1 ...
- Hibernate(11)_基于外键的双向1对1
一.基于外键的双向1对1 对于基于外键的1-1关联,其外键可以存放在任意一边,在需要存放外键一端,增加many-to-one元素.为many-to-one元素增加unique="true&q ...
- 【面试 spring boot】【第十七篇】spring boot相关面试
spring boot相关面试 ====================================================== 1.spring boot启动类 启动原理 参考:htt ...