使用 LINQ to XML 生成动态 sitemap.xml 时如何正确生成 xsi:schemalocation 属性?

发布于 2024-09-11 07:10:39 字数 1307 浏览 5 评论 0原文

我正在生成一个动态 sitemap.xml

根据 sitemaps.org 这是站点地图的标题.xml

<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    ...
    </url>
</urlset>

所以我使用 LINQ To XML 来生成 sitemap.xml

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
return new XElement(ns + "urlset",
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    //new XAttribute("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in new GetNodes()
    select new XElement(ns + "url",
        new XElement(ns + "loc", node.Loc),
        new XElement(ns + "lastmod", node.LastMod),
        new XElement(ns + "priority", node.Priority)
    )
).ToString();

注释行是我无法正确理解的行。
如何设置“xsi:schemalocation”属性?

谢谢。

I am generating a dynamic sitemap.xml

According to sitemaps.org this is the header for a sitemap.xml

<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
     xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
    ...
    </url>
</urlset>

So I'm using LINQ To XML to generate the sitemap.xml

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
return new XElement(ns + "urlset",
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    //new XAttribute("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in new GetNodes()
    select new XElement(ns + "url",
        new XElement(ns + "loc", node.Loc),
        new XElement(ns + "lastmod", node.LastMod),
        new XElement(ns + "priority", node.Priority)
    )
).ToString();

The commented line is the one i cannot get right.
How can i set the "xsi:schemalocation" attribute?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风无影 2024-09-18 07:10:40

好吧,我猜对了。感谢迈克·卡伦
如果我声明 XAtrribute(XNamespace.Xmlns + "xsi",...) 那么它就可以工作

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
return new XElement(ns + "urlset",  
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in GetNodes() 
    select new XElement(ns + "url", 
        new XElement(ns + "loc", node.Loc), 
        new XElement(ns + "lastmod", node.LastMod), 
        new XElement(ns + "priority", node.Priority) 
    ) 
).ToString(); 

Ok, I got it right. Thanks to Mike Caron
If I declare the XAtrribute(XNamespace.Xmlns + "xsi",...) then it works

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9"; 
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance"; 
return new XElement(ns + "urlset",  
    new XAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9"),
    new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in GetNodes() 
    select new XElement(ns + "url", 
        new XElement(ns + "loc", node.Loc), 
        new XElement(ns + "lastmod", node.LastMod), 
        new XElement(ns + "priority", node.Priority) 
    ) 
).ToString(); 
生寂 2024-09-18 07:10:40

我不知道 LINQ to XML,但快速浏览一下文档后,请尝试以下操作:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
return new XElement(ns + "urlset",
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in new GetNodes()
    select new XElement(ns + "url",
        new XElement(ns + "loc", node.Loc),
        new XElement(ns + "lastmod", node.LastMod),
        new XElement(ns + "priority", node.Priority)
    )
).ToString();

请注意,我没有显式设置 xmlns 属性。我怀疑它们是自动生成的。另外,买者自负,因为这还没有经过测试。

I don't know LINQ to XML, but after a quick peek at the documentation, try this:

XNamespace ns = "http://www.sitemaps.org/schemas/sitemap/0.9";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
return new XElement(ns + "urlset",
    new XAttribute(xsi + "schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"),
    from node in new GetNodes()
    select new XElement(ns + "url",
        new XElement(ns + "loc", node.Loc),
        new XElement(ns + "lastmod", node.LastMod),
        new XElement(ns + "priority", node.Priority)
    )
).ToString();

Note that I'm not setting the xmlns attributes explicitly. I suspect they're generated automatically. Also, caveat emptor, since this is not tested.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文