方式一、

 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的更多相关文章

  1. 创建Google网站地图Sitemap.xml

    Sitemap.xml是google搞出来的,也就是网站地图,不过这个网站地图是用xml写的,而且要按google的标准来写,并且要将写出来的这个文件sitemap.xml上传到自己的服务器空间中去. ...

  2. ecshop 改变sitemap.xml的位置

    大家知道ECSHOP默认的sitemap.xml文件是放置在data文件夹中的,但是这不利于GOOGLE的抓取.我们必须把sitemap.xml文件放置在根目录下 在admin/sitemap.php ...

  3. ecshop优化修改sitemap.xml到根目录

    大家都知道sitemap.xml是用来给搜索引擎提交收录的工具,虽然搜索引擎自己也会收录网站但是有了sitemap.xml之后速度会加快不少.而ecshop程序是有自动生成sitemap.xml的功能 ...

  4. sitemap.xml 静态和动态生成页面 shopnc二次开发 动态生成sitemap.xml

    Sitemap 可方便网站管理员通知搜索引擎他们网站上有哪些可供抓取的网页.最简单的 Sitemap 形式,就是XML 文件,在其中列出网站中的网址以及关于每个网址的其他元数据(上次更新的时间.更改的 ...

  5. google sitemap

    引言 刚开始以为要一个绿色快速通道网页,涵盖常用的地址链接,以便于google的爬虫统计数据,然后看了google sitemap站点文档,原来站点地图是一种文件,您可以通过该文件列出您网站上的网页, ...

  6. sitemap xml文件生成

    sitemap xml生成方法 <?php /** * SitemapService.php. * * 生成sitemap */ class Sitemap { public $newLine ...

  7. 帝国CMS如何自动生成sitemap.xml网站地图文件

    登录网站的后台http://你的域名/e/admin/ 进入后台栏目 =>增加自定义页面 =>选择直接页面,页面名称为:网站地图,文件名修改为  ../../sitemap.xml 内容填 ...

  8. 多语言的sitemap xml

    请注意一下 sitemap xml 也有多语言的

  9. php生成百度站点地图sitemap.xml

    <?php header("Content-type:text/html;charset=utf-8"); //php生成百度站点地图sitemap.xml //http:/ ...

随机推荐

  1. Ignatius and the Princess III HDU - 1028 -生成函数or完全背包计数

    HDU - 1028 step 1:初始化第一个多项式 也就是 由 1的各种方案 组 成 的多项式 初始化系数为 1.临时区 temp初始化 为 0 step 2:遍历后续的n - 1 个 多项式 , ...

  2. SpringBoot使用Mybatis-PageHelper

    前言 之前一篇文章介绍了<SpringBoot+Mybatis+MySql学习>的整合,这一片扩展一下Mybatis的分页插件-Mybatis-PageHelper. 新建项目 首先,po ...

  3. C#选择文件、选择文件夹、打开文件

    1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以 ...

  4. Linux下nginx的使用

    day02 .远程连接服务器故障排查 )道路是否通畅 )是否有劫财劫色 xxxxxx no route to host xxxx )洗浴中心是否提供你想要的服务 sshd远程连接服务 == ip 端口 ...

  5. BZOJ4039 : 集会

    将曼哈顿距离转化为切比雪夫距离,即: $|x_1-x_2|+|y_1-y_2|=\max(|(x_1+y_1)-(x_2+y_2)|,|(x_1-y_1)-(x_2-y_2)|)$ 那么每个点能接受的 ...

  6. C++学习笔记55:流类库与输入输出

    I/O流的概念及流类库的结构 程序与外界环境的信息交换 当程序与外界环境进行信息交换时,存在着两个对象:程序中的对象,文件对象. 流是一种抽象,负责在数据的生产者和数据的消费者之间建立联系,并管理数据 ...

  7. sqlmap简单使用

    就以实验吧上那个简单的sql注入题为例吧,不过那道题确实经典,把sqlmap的整个使用过程都展现了一遍,先奉上那道题的地址:http://ctf5.shiyanbar.com/web/index_3. ...

  8. Servlet(5)—ServletRequest接口和ServletResponse接口

    ServletRequest接口: 使用ServletRequest接口创建对象,用于使客户端请求信息对Servlet可用,创建的对象作为参数传递之Servlet的Service() ServletR ...

  9. Deepin 15.4 安装 Double Commander

    参照官方教程,安装命令行如下: https://software.opensuse.org/download.html?project=home%3AAlexx2000&package=dou ...

  10. dubbo+zipkin调用链监控(二)

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...