如何使用 XDocument 创建具有多个名称空间的 Xml

发布于 11-08 15:11 字数 1513 浏览 2 评论 0原文

我想创建一个具有多个命名空间的文档(请参见下面的 Xml)。我如何使用 XDocument 来做到这一点?我的最终结果应该如下:

<?xml version="1.0" encoding="utf-8"?>
<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02">
  <a:link rel="prev" type="application/atom+xml" href="myUrl" />
  <a:link rel="next" type="application/atom+xml" href="myUrl" />
  <a:link rel="self" type="application/atom+xml" href="myUrl" />
  <a:updated>2008-05-20T22:50:46.7932864Z</a:updated>

这是我到目前为止的代码。

    XNamespace nsW3Atom = "http://www.w3.org/2005/Atom";
    XNamespace nsOs = "http://a9.com/-/spec/opensearch/1.1/";
    XNamespace nsZune = "http://schemas.zune.net/calatog/apps/2008/02";

    XDocument doc =
        new XDocument(
            // new XDeclaration("1.0", "utf-8", "no"),
            new XElement("feed",
                new XAttribute("a", nsW3Atom),
                new XAttribute("os", nsOs),
                new XAttribute("os2", nsZune),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "prev"), new XAttribute("type", "application/atom+xml")),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "next"), new XAttribute("type", "application/atom+xml")),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "self"), new XAttribute("type", "application/atom+xml"))));

预先感谢您的帮助!

I would like to create a document with multiple namespaces (see Xml below). How can I do that using XDocument? My final result should the following:

<?xml version="1.0" encoding="utf-8"?>
<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns:os="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://schemas.zune.net/catalog/apps/2008/02">
  <a:link rel="prev" type="application/atom+xml" href="myUrl" />
  <a:link rel="next" type="application/atom+xml" href="myUrl" />
  <a:link rel="self" type="application/atom+xml" href="myUrl" />
  <a:updated>2008-05-20T22:50:46.7932864Z</a:updated>

Here is the code I have so far.

    XNamespace nsW3Atom = "http://www.w3.org/2005/Atom";
    XNamespace nsOs = "http://a9.com/-/spec/opensearch/1.1/";
    XNamespace nsZune = "http://schemas.zune.net/calatog/apps/2008/02";

    XDocument doc =
        new XDocument(
            // new XDeclaration("1.0", "utf-8", "no"),
            new XElement("feed",
                new XAttribute("a", nsW3Atom),
                new XAttribute("os", nsOs),
                new XAttribute("os2", nsZune),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "prev"), new XAttribute("type", "application/atom+xml")),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "next"), new XAttribute("type", "application/atom+xml")),
                new XElement(XNamespace.Xmlns + "link", new XAttribute("rel", "self"), new XAttribute("type", "application/atom+xml"))));

Thank you in advance for the help!

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

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

发布评论

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

评论(2

百思不得你姐2024-11-15 15:11:19
 XNamespace a="http://www.w3.org/2005/Atom";
 XNamespace os = "http://a9.com/-/spec/opensearch/1.1/";
 XNamespace def = "http://schemas.zune.net/catalog/apps/2008/02";

 var doc =
     new XDocument(new XElement(a + "feed", 
                         new XAttribute(XNamespace.Xmlns + "a", a),
                         new XAttribute(XNamespace.Xmlns + "os", os),
                         new XAttribute("xmlns", def)));

那么如果您想使用 a 命名空间,请在任何元素前加上 a+ 前缀

 XNamespace a="http://www.w3.org/2005/Atom";
 XNamespace os = "http://a9.com/-/spec/opensearch/1.1/";
 XNamespace def = "http://schemas.zune.net/catalog/apps/2008/02";

 var doc =
     new XDocument(new XElement(a + "feed", 
                         new XAttribute(XNamespace.Xmlns + "a", a),
                         new XAttribute(XNamespace.Xmlns + "os", os),
                         new XAttribute("xmlns", def)));

then if You want to use the a Namespace, prefix any element with a+

说好的呢2024-11-15 15:11:19

创建属性时使用 xmlns: 前缀。

            new XAttribute("xmlns:a", nsW3Atom),
            new XAttribute("xmlns:os", nsOs),
            new XAttribute("xmlns:os2", nsZune)

When creating the attributes use the xmlns: prefix.

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