属性上的XElement默认名称空间提供了意外的行为 [英] XElement default namespace on attributes provides unexpected behaviour

查看:93
本文介绍了属性上的XElement默认名称空间提供了意外的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建包含默认名称空间和命名名称空间的XML文档时遇到了麻烦,仅显示我要生成的内容很难解释...

I am having trouble creating an XML document that contains a default namespace and a named namespace, hard to explain easier to just show what I am trying to produce...

<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>

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

but what I end up with is this...

<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代码段的代码是...

The code that I wrote to produce this XML snippet is this...

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

基于MSDN上的示例

****编辑****

****EDIT****

好吧,通过更多的实验,我现在对XML名称空间的工作方式非常困惑.

Ok, with some more experimentation I am now very confused on how XML namespaces work....

如果我删除aw + theattribute,我会得到我想要的...但是现在看来,我所追求的实际上并不是我所期望的.我以为命名空间是从其父项继承而来的,这对属性也不适用吗?因为,这段读取属性的代码无法正常工作...

if I remove the aw + theattribute I get what I was after...but now it seems that what I was after is not actually what I expected. I thought that namespaces were inherited from their parents, is this not true of attributes as well? because, this code to read the attributes does not work as I expected...

  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 +,就可以了,这使我假设我不能在默认名称空间中拥有属性.这是正确的吗?

However if I remove the aw + on the attribute its ok, leading me to assume that I cannot have attributes in the default namespace. Is this correct?

推荐答案

好问题.我仔细研究了一下,发现这部分XML规范 :

Good question. I dug around a bit, and found 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>

因此,基本上,属性名称默认情况下不会获得名称空间,这解释了您所看到的所有内容:)

So basically, it looks like attribute names don't get namespaces by default, which explains everything you've seen :)

这篇关于属性上的XElement默认名称空间提供了意外的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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