如何生成XSI:正确的schemaLocation属性与LINQ生成一个动态的sitemap.xml到XML时? [英] How to generate xsi:schemalocation attribute correctly when generating a dynamic sitemap.xml with LINQ to XML?

查看:468
本文介绍了如何生成XSI:正确的schemaLocation属性与LINQ生成一个动态的sitemap.xml到XML时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成一个动态的sitemap.xml

I am generating a dynamic sitemap.xml

根据站点地图。组织这是一个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>



所以我使用LINQ to XML生成的sitemap.xml

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();



在注释行是一个我无法得到正确的。结果
我怎样才能设置XSI:的schemaLocation?属性

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

感谢

推荐答案

好吧,我是正确的。感谢Mike卡隆结果
。如果我宣布XAtrribute(XNamespace.Xmlns +XSI,...),那么它的工作原理

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(); 

这篇关于如何生成XSI:正确的schemaLocation属性与LINQ生成一个动态的sitemap.xml到XML时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆