如何仅在第一个音符中添加 <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
how to add <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> in only first note
我想为 google 创建 sitemap.xml,当我使用“ns+”XNamespace 插入所有节点并且输出在 xml 代码下 xmlns="" 是缺少,如何创建,请帮助我使用 asp.net 和 c# 不创建站点地图
我使用 foreach 读取所有新闻数据并动态创建 url 个节点
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url xmlns="">
<loc>https://test.com/</loc>
<lastmod>2022-05-21</lastmod>
<priority>1.0</priority>
</url>
</urlset>
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XDocument xDoc = new XDocument();
var element = new XElement(ns + "urlset");
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/"),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/NewsList.aspx"),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/NewsList.aspx?pn=0"),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
foreach (var item in newsIds)
{
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/news.aspx?id=" + item),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
}
xDoc.Add(element);
xDoc.Save(HttpContext.Current.Server.MapPath("~") + "sitemap.xml");
我在这里找到了解决方案:
How can I remove empty xmlns attribute from node created by XElement
var element = new XElement(ns + "urlset");
element.Add(new XElement(ns + "url",
//Root Element
new XElement(ns + "loc", "https://test.com/"),
new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement(ns + "priority", "1.0"))
);
我想为 google 创建 sitemap.xml,当我使用“ns+”XNamespace 插入所有节点并且输出在 xml 代码下 xmlns="" 是缺少,如何创建,请帮助我使用 asp.net 和 c# 不创建站点地图 我使用 foreach 读取所有新闻数据并动态创建 url 个节点
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url xmlns="">
<loc>https://test.com/</loc>
<lastmod>2022-05-21</lastmod>
<priority>1.0</priority>
</url>
</urlset>
XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XDocument xDoc = new XDocument();
var element = new XElement(ns + "urlset");
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/"),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/NewsList.aspx"),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/NewsList.aspx?pn=0"),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
foreach (var item in newsIds)
{
element.Add(new XElement("url",
//Root Element
new XElement("loc", "https://test.com/news.aspx?id=" + item),
new XElement("lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement("priority", "1.0"))
);
}
xDoc.Add(element);
xDoc.Save(HttpContext.Current.Server.MapPath("~") + "sitemap.xml");
我在这里找到了解决方案:
How can I remove empty xmlns attribute from node created by XElement
var element = new XElement(ns + "urlset");
element.Add(new XElement(ns + "url",
//Root Element
new XElement(ns + "loc", "https://test.com/"),
new XElement(ns + "lastmod", String.Format("{0:yyyy-MM-dd}", DateTime.Now)),
new XElement(ns + "priority", "1.0"))
);