简体   繁体   English

为什么我在有效的XML上获得XmlException?

[英]Why am I getting an XmlException on valid XML?

In the process of testing a method that accepts a string and adds that string as an attribute to an XElement (with the current time as a value). 在测试接受字符串的方法并将该字符串作为属性添加到XElement(以当前时间作为值)的过程中。 To use XElement.SetAttributeValue(XName name, object value) (the only way I'm aware of to add/update an attribute of an XElement), I'm using XName.Get to convert the provided string to an XName. 要使用XElement.SetAttributeValue(XName name, object value) (我知道添加/更新XElement属性的唯一方法),我使用XName.Get将提供的字符串转换为XName。

The problem I'm running into is that XName.Get (stack trace shows it is coming from System.Xml.XmlConvert.VerifyNCName ) is throwing an XmlException in some cases, claiming certain characters aren't allowed. 遇到 的问题是 XName.Get (堆栈跟踪显示它来自System.Xml.XmlConvert.VerifyNCName )在某些情况下抛出XmlException,声称不允许某些字符。 One example is 0x02FF (˱). 一个例子是0x02FF(˱)。 I wouldn't have expected that this character is allowed, but the XML specification seems to say the whole range of 0x00F8 to 0x02FF is valid. 我不希望这个字符被允许,但XML规范似乎说整个范围0x00F8到0x02FF是有​​效的。

Am I getting this exception on valid characters, or am I misunderstanding the spec? 我是否在有效字符上获得此异常,或者我是否误解了规范?

AFAIR Xml stack in .NET Framework 4 and .NET Framework 4.5 is based on the 4th edition of the Xml spec and not 5th. .NET Framework 4和.NET Framework 4.5中的AFAIR Xml堆栈基于Xml规范的第4版,而不是第5版。 When you look at this spec you will notice that not all valid characters are allowed in names. 当您查看此规范时,您会注意到名称中不允许所有有效字符。 Especially take a look at this and look what a Letter is and you will see that characters from range [0x02C2-0x0385] are excluded. 特别是看看这个 ,看看是什么字母 ,你会看到范围[0x02C2-0x0385]中的字符被排除在外。

Also the reason why Xml stack in .NET Framework 4 and 4.5 has not moved to the 5th edition was that due to changes in character ranges the same Xml document could either be valid or invalid (this is actually the issue you hit) depending on the processor and not on the document itself (Xml documents that comply to 5th edition still could have version 1.0). 此外,.NET Framework 4和4.5中的Xml堆栈尚未移至第5版的原因是,由于字符范围的更改,相同的Xml文档可能有效或无效(这实际上是您遇到的问题),具体取决于处理器而不是文档本身(符合第5版的Xml文档仍然可以有1.0版本)。 So valid documents (that comply with th 5th edition) would be rejected by older parsers as invalid. 因此,有效的文档(符合第5版)将被旧解析器拒绝为无效。 It also did not work the other way around. 它也没有相反的方式。 If the 5th edition capable Xml parser received a document with a character in the name that was not valid before but was made valid in the 5th edition it was impossible to tell whether this document should be rejected as one that was supposed to be pre-5th edition and is malformed or it was a 5th edition document and should be accepted. 如果具有第5版能力的Xml解析器收到的文件的名称中的字符在之前无效但在第5版中生效,则无法判断该文档是否应该被拒绝为本应该在5之前的文档。版本和格式不正确或它是第5版文件,应该被接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM