gpt4 book ai didi

c# - 属性上的 XElement 默认命名空间提供意外行为

转载 作者:行者123 更新时间:2023-11-30 14:20:32 26 4
gpt4 key购买 nike

我在创建包含默认 namespace 和命名 namespace 的 XML 文档时遇到了问题,很难解释得更容易,只是展示我正在尝试生成的内容...

<Root xmlns="http://www.adventure-works.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.SomeLocatation.Com/MySchemaDoc.xsd">
<Book title="Enders Game" author="Orson Scott Card" />
<Book title="I Robot" author="Isaac Asimov" />
</Root>

但我最终得到的是这个......

<Root xmlns="http://www.adventure-works.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.SomeLocatation.Com/MySchemaDoc.xsd">
<Book p3:title="Enders Game" p3:author="Orson Scott Card" xmlns:p3="http://www.adventure-works.com" />
<Book p3:title="I Robot" p3:author="Isaac Asimov" xmlns:p3="http://www.adventure-works.com" />
</Root>

我为生成此 XML 片段而编写的代码是这样的...

  XNamespace aw = "http://www.adventure-works.com";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XElement root = new XElement(aw + "Root",
new XAttribute("xmlns", "http://www.adventure-works.com"),
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
new XAttribute(xsi + "SchemaLocation", "http://www.SomeLocatation.Com/MySchemaDoc.xsd"),

new XElement(aw + "Book",
new XAttribute(aw + "title", "Enders Game"),
new XAttribute(aw + "author", "Orson Scott Card")),
new XElement(aw + "Book",
new XAttribute(aw + "title", "I Robot"),
new XAttribute(aw + "author", "Isaac Asimov")));

基于 example on MSDN

****编辑****

好吧,通过更多的实验,我现在对 XML namespace 的工作方式感到非常困惑....

如果我删除 aw + theattribute,我会得到我想要的……但现在看来我想要的并不是我所期望的。我以为命名空间是从它们的父级继承的,属性不也是这样吗?因为,这段读取属性的代码并没有像我预期的那样工作...

  XElement xe = XElement.Parse(textBox1.Text);
XNamespace aw = "http://www.adventure-works.com";
var qry = from x in xe.Descendants(aw + "Book")
select (string)x.Attribute(aw + "author");

但是,如果我删除属性上的 aw + 就可以了,这让我假设我不能在默认命名空间中拥有属性。这是正确的吗?

最佳答案

好问题。我挖了一下,发现this bit of the XML spec :

A default namespace declaration applies to all unprefixed element names within its scope. Default namespace declarations do not apply directly to attribute names; the interpretation of unprefixed attributes is determined by the element on which they appear.

后面继续举这个例子:

For example, each of the bad empty-element tags is illegal in the following:

<!-- http://www.w3.org is bound to n1 and n2 -->
<x xmlns:n1="http://www.w3.org"
xmlns:n2="http://www.w3.org" >
<bad a="1" a="2" />
<bad n1:a="1" n2:a="2" />
</x>

However, each of the following is legal, the second because the default namespace does not > apply to attribute names:

<!-- http://www.w3.org is bound to n1 and is the default -->
<x xmlns:n1="http://www.w3.org"
xmlns="http://www.w3.org" >
<good a="1" b="2" />
<good a="1" n1:a="2" />
</x>

基本上,看起来属性名称默认情况下没有命名空间,这解释了您所看到的一切:)

关于c# - 属性上的 XElement 默认命名空间提供意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1231396/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com