将带名称和不带名称的命名空间添加到 XElement

发布于 2024-09-04 20:27:05 字数 1635 浏览 3 评论 0原文

我需要生成如下所示的 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 技术交流群。

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

发布评论

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

评论(1

多情出卖 2024-09-11 20:27:05

您需要将“空白”命名空间声明为默认命名空间。例如,这工作得很好:

        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("xmlns", blank.NamespaceName), 
                new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),

                new XElement(blank + "url",
                    new XElement(blank + "loc", "http://www.xyz.eu/"),
                    new XElement(blank + "lastmod", "2010-01-20T10:56:47Z"),
                    new XElement(blank + "changefreq", "daily"),
                    new XElement(blank + "priority", "1"))
             ));

        Console.WriteLine(doc.ToString());

You need to declare the "blank" namespace as the default namespace. For example this works just fine:

        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("xmlns", blank.NamespaceName), 
                new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),

                new XElement(blank + "url",
                    new XElement(blank + "loc", "http://www.xyz.eu/"),
                    new XElement(blank + "lastmod", "2010-01-20T10:56:47Z"),
                    new XElement(blank + "changefreq", "daily"),
                    new XElement(blank + "priority", "1"))
             ));

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