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:/ ...
随机推荐
- vue实例属性之methods和computed
我们可以把同一函数放在methods或者computed中,区别在于computed有缓存,计算属性只有在它的相关依赖发生改变时才会重新求值,即数据改变才会执行函数.而methods每当触发重新渲染时 ...
- 2017-9-8-visio制作lcd液晶背景
看到别人的帖子有用visio做tft的背景图片的,十分感兴趣,电脑上也有visio,搞起.. 按照下图找到合适的模板(visio2010版本,其他版本应该会略有不同). 拖动界面左侧的各种丰富的小插 ...
- BZOJ4962 : 简单的字符串
枚举子串的中心,往两侧扩展,将两侧对应位置的字符交替写下来,得到一个字符串$S$. 若前后长度为$L$的子串循环同构,则在$S$中它们对应长度为$2L$的前缀,需要满足它可以由不超过$2$个偶回文串拼 ...
- BZOJ3795 : 魏总刷DP
对于HARD: 需要满足$k+u[i]\times k\leq T+late[i]$. 对于EASY: 需要满足$k+u[i]\times k\leq T-rest[i]$. 故对于HARD,设$a[ ...
- [P1020]导弹拦截 (贪心/DP/二分/单调队列)
一道很经典的题 这道题就是要求一个最长单调不升子序列和一个最长单调上升子序列. 先打了一个n2复杂度的 用DP #include<bits/stdc++.h> using namespac ...
- UOJ#132&bzoj4200[Noi2015]小园丁与老司机
看,这是一个传送门 Part A 把坐标离散化,按照纵坐标为第一关键字,横坐标为第二关键字排序 以$f_i$记录来到$i$这个点最多经过点数,那么答案显而易见就是$f_i$加上该层点数 转移的话就是分 ...
- SourceTree安装教程和破解教程
SourceTree破解版是一款非常实用的编程工具,这是一款专业的Git和Hg客户端,界面简洁,操作简单易上手,是开发者的必备工具,欢迎大家来绿色资源网下载体验!SourceTree是一款免费的Git ...
- px与rem的换算
在线转化工具: http://www.ofmonkey.com/front/rem rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个参考值,这个参考值设置为多少, ...
- 使用Logstash filter grok过滤日志文件
Logstash提供了一系列filter过滤plugin来处理收集到的log event,根据log event的特征去切分所需要的字段,方便kibana做visualize和dashboard的da ...
- Kafka中时间轮分析与Java实现
在Kafka中应用了大量的延迟操作但在Kafka中 并没用使用JDK自带的Timer或是DelayQueue用于延迟操作,而是使用自己开发的DelayedOperationPurgatory组件用于管 ...