在 C# 中,有没有办法使用短前缀而不是每个节点的完整命名空间来生成 XDocument?

发布于 2024-11-24 15:22:35 字数 879 浏览 2 评论 0原文

我只是想让我的 XML 更整洁、更简洁。我知道在 C# 中可以做这样的事情:

XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
new XElement(ds + "MyDumbElementName", "SomethingStupid");

并获得一个与此类似的 XML: 而

<root>
    <MyDumbElementName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
        SomethingStupid
    </MyDumbElementName>
</root>

不是这样的事情:

<root xmlns:ds="http://schemas.microsoft.com/ado/2007/08/dataservices">
    <ds:MyDumbElementName>
        SomethingStupid
    </ds:MyDumbElementName>
</root>

显然,第二个版本更漂亮,更易于阅读且紧凑。有没有办法生成相当于紧凑版本的 XDocument,而不调用 Parse("...")?

你可能决定冒险回答“不”,在这种情况下,我相信公平的做法是等待其他人回答,如果没有人给出像样的答案,我会接受你的“不”,否则如果有人确实提供了答案,我会记下“否”。我希望这对你来说也很公平。

编辑:也许我应该更具体一点,说我希望能够使用多个名称空间,而不仅仅是一个。

I'm simply trying to make my XML a bit tidier and less bulky. I know in C# one can do something like this:

XNamespace ds = "http://schemas.microsoft.com/ado/2007/08/dataservices";
new XElement(ds + "MyDumbElementName", "SomethingStupid");

And get an XML simliar to this:

<root>
    <MyDumbElementName xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices">
        SomethingStupid
    </MyDumbElementName>
</root>

Instead of something like this:

<root xmlns:ds="http://schemas.microsoft.com/ado/2007/08/dataservices">
    <ds:MyDumbElementName>
        SomethingStupid
    </ds:MyDumbElementName>
</root>

Obviously the second version is much prettier, easier to read, and compact. Is there any way to generate an XDocument equivalent of the compact version, without calling Parse("...")?

You may decide to take a risk and answer "No", in which case I believe the fair thing to do is wait for other people to answer, and if no-one gives a decent answer I'll accept your "No", otherwise if someone does provide an answer, I'll mark the "No" down. I hope that seems fair to you too.

EDIT: Perhaps I should be a bit more specific and say that I want to be able to use multiple name spaces, not just one.

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

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

发布评论

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

评论(1

末が日狂欢 2024-12-01 15:22:35

您可以通过指定 xmlns 属性显式覆盖此行为:

XNamespace ns = "urn:test";

new XDocument (
    new XElement ("root",
        new XAttribute (XNamespace.Xmlns + "ds", ns),
        new XElement (ns + "foo",
            new XAttribute ("xmlns", ns),
            new XElement (ns + "bar", "content")
        ))
).Dump ();

<root xmlns:ds="urn:test">
  <foo xmlns="urn:test">
    <bar>content</bar>
  </foo>
</root> 

默认情况下,该行为是内联指定 xmlns。

XNamespace ns = "urn:test";

new XDocument (
    new XElement ("root",
        new XElement (ns + "foo",
            new XElement (ns + "bar", "content")
        ))
).Dump ();

给出输出:

<root>
  <foo xmlns="urn:test">
    <bar>content</bar>
  </foo>
</root>

因此,默认行为是您想要的行为,除非已经定义了命名空间:

XNamespace ns = "urn:test";

new XDocument (
    new XElement ("root",
        new XAttribute (XNamespace.Xmlns + "ds", ns),
        new XElement (ns + "foo",
            new XElement (ns + "bar", "content")
        ))
).Dump ();

<root xmlns:ds="urn:test">
  <ds:foo>
    <ds:bar>content</ds:bar>
  </ds:foo>
</root> 

You can explicitly override this behaviour by specifying the xmlns attribute:

XNamespace ns = "urn:test";

new XDocument (
    new XElement ("root",
        new XAttribute (XNamespace.Xmlns + "ds", ns),
        new XElement (ns + "foo",
            new XAttribute ("xmlns", ns),
            new XElement (ns + "bar", "content")
        ))
).Dump ();

<root xmlns:ds="urn:test">
  <foo xmlns="urn:test">
    <bar>content</bar>
  </foo>
</root> 

By default the behaviour is to specify the xmlns in-line.

XNamespace ns = "urn:test";

new XDocument (
    new XElement ("root",
        new XElement (ns + "foo",
            new XElement (ns + "bar", "content")
        ))
).Dump ();

Gives the output:

<root>
  <foo xmlns="urn:test">
    <bar>content</bar>
  </foo>
</root>

So the default behaviour is your desired behaviour, except when the namespace is already defined:

XNamespace ns = "urn:test";

new XDocument (
    new XElement ("root",
        new XAttribute (XNamespace.Xmlns + "ds", ns),
        new XElement (ns + "foo",
            new XElement (ns + "bar", "content")
        ))
).Dump ();

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