简体   繁体   中英

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

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. One example is 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.

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

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). So valid documents (that comply with th 5th edition) would be rejected by older parsers as invalid. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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