XmlAttribute :: AttributeName(DotNet)中分号(;)的用途是什么

时间:2010-11-04 15:58:17

标签: c# xml

我正试图解决一个编程之谜(对我来说)。我已经搜索过谷歌,但我没有发现在XmlAttribute的AttributeName属性中使用分号。我正在使用一个序列化对象的应用程序。当序列化此对象时,所有属性都具有与后缀相同的值。

例如:

[XmlType(TypeName = "Foo", Namespace = Declarations.SchemaVersion), XmlRoot, Serializable]
public class Foo
{
    private string _Name;

    [XmlAttribute(AttributeName = "Name;", Form = XmlSchemaForm.Unqualified, DataType = "string", Namespace = Declarations.SchemaVersion)]
    public string Name
    {
        get
        {
            return this._Name;
        }
        set
        {
            this._Name = value;
        }
    }
}

获取序列化为:

<Foo Name_x003B_="John" />

我的问题是,这个 x003B 来自哪里(我在代码中搜索了文字“ x003B ”但没有找到任何内容{上面只是一个例子,我正在使用一个很大的代码库})。我在哪里可以更改它? AttributeName末尾的分号的用途是什么?谢谢!

4 个答案:

答案 0 :(得分:2)

这是; = _x003B_

的编码值

答案 1 :(得分:2)

XmlSerializer通过将字符放在下划线中来对字符(如分号)进行编码,其中字符的十六进制值为name;变成Name_0x003B_。如果你在那里放一个问号,那就是Name_0x003F _。

答案 2 :(得分:1)

我很惊讶它被允许作为XML属性名称中的单个字符。 XML不允许:

<Foo Name;="bar"/>

来自规范:

The first character of a Name MUST be a NameStartChar, and any other characters MUST be NameChars; this mechanism is used to prevent names from beginning with European (ASCII) digits or with basic combining characters. Almost all characters are permitted in names, except those which either are or reasonably could be used as delimiters. The intention is to be inclusive rather than exclusive, so that writing systems not yet encoded in Unicode can be used in XML names. See J Suggestions for XML Names for suggestions on the creation of names.

Document authors are encouraged to use names which are meaningful words or combinations of words in natural languages, and to avoid symbolic or white space characters in names. Note that COLON, HYPHEN-MINUS, FULL STOP (period), LOW LINE (underscore), and MIDDLE DOT are explicitly permitted.

The ASCII symbols and punctuation marks, along with a fairly large group of Unicode symbol characters, are excluded from names because they are more useful as delimiters in contexts where XML names are used outside XML documents; providing this group gives those contexts hard guarantees about what cannot be part of an XML name. The character #x037E, GREEK QUESTION MARK, is excluded because when normalized it becomes a semicolon, which could change the meaning of entity references.
Names and Tokens
[4]    NameStartChar    ::=    ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
[4a]    NameChar    ::=    NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
[5]    Name    ::=    NameStartChar (NameChar)*
[6]    Names    ::=    Name (#x20 Name)*
[7]    Nmtoken    ::=    (NameChar)+
[8]    Nmtokens    ::=    Nmtoken (#x20 Nmtoken)*

似乎发生的事情是系统已翻译禁用字符';'一组字符,可由人类解释,但不能解码为Unicode代码点。我不认为这是所有XML实现的标准行为。

我还怀疑它可能是一个错误,因为属性名称为“Name;”可能会在某些XML工具中引起问题。

答案 3 :(得分:0)

不是来自

AttributeName = "Name;"