将带名称和不带名称的命名空间添加到 XElement
我需要生成如下所示的 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<url>
<loc>http://www.xyz.eu/</loc>
<lastmod>2010-01-20T10:56:47Z</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
<url>
<loc>http://www.xyz.eu/2/</loc>
<lastmod>2009-10-13T10:20:03Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.xyz.eu/3/</loc>
<lastmod>2009-10-13T10:19:09Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
</urlset>
我似乎无法弄清楚如何添加没有名称的命名空间而不在所有 url 标签中放入“ xmlns="” ”。
我的代码:
XNamespace blank = XNamespace.Get(@"http://www.sitemaps.org/schemas/sitemap/0.9");
XNamespace xsi = XNamespace.Get(@"http://www.w3.org/2001/XMLSchema-instance");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(blank + "urlset",
//new XAttribute(XNamespace.Xmlns +"", blank),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
// This private method loops through the dictionary and creates all the page nodes
GetSiteMapChildren(pageIdVersionDic, site.Url)
));
有什么想法吗?谢谢
I need so generate XML like the following:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<url>
<loc>http://www.xyz.eu/</loc>
<lastmod>2010-01-20T10:56:47Z</lastmod>
<changefreq>daily</changefreq>
<priority>1</priority>
</url>
<url>
<loc>http://www.xyz.eu/2/</loc>
<lastmod>2009-10-13T10:20:03Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
<url>
<loc>http://www.xyz.eu/3/</loc>
<lastmod>2009-10-13T10:19:09Z</lastmod>
<changefreq>daily</changefreq>
<priority>0.5</priority>
</url>
</urlset>
I cant seem to figure out how to add the namespace with no name without putting ' xmlns="" ' in all the url tags.
my code:
XNamespace blank = XNamespace.Get(@"http://www.sitemaps.org/schemas/sitemap/0.9");
XNamespace xsi = XNamespace.Get(@"http://www.w3.org/2001/XMLSchema-instance");
XDocument doc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement(blank + "urlset",
//new XAttribute(XNamespace.Xmlns +"", blank),
new XAttribute(XNamespace.Xmlns + "xsi", xsi),
// This private method loops through the dictionary and creates all the page nodes
GetSiteMapChildren(pageIdVersionDic, site.Url)
));
Any ideas? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将“空白”命名空间声明为默认命名空间。例如,这工作得很好:
You need to declare the "blank" namespace as the default namespace. For example this works just fine: