Is XML Serialization in .Net case sensitive?

夙愿已清 提交于 2019-12-24 16:27:30

问题


I did not find any documentation which explicitly states whether XML serialization is case-sensitive or not.

Suppose I have following class:

class Test
{
public int Field;
public string field;
}

Will I have any problems when XML serializing this class becuase it contains two fields having same name differing only in case?

CLARIFICATION: I KNOW its bad to have two fields with same name differing only by case and i am NOT designing such a beast. I am battling a beast let loose on me by another person.


回答1:


The short answer is that serialization preserves case and the class will be successfully serialized in C# (assuming you make the class public - as is, the XmlSerializer will choke on it).

But if you have to ask this question at all, then there's something seriously wrong with the class design. It is a horrible, horrible thing to have two public properties or fields, with the same name differing only by case, that each perform or affect different functionality. In this case, the fields aren't even the same type.

If you downloaded a 3rd-party library and it handed you a class like this, what would you do? How would you know which is which? How would you even document this, or look it up in the documentation? Just don't do it - don't have two members that have, for all intents and purposes, the same name. The only exception to this is a private backing field for a public property - in that case, the two are very closely related, so it's not as much of a problem (although some people still prefer to prefix with underscores).

And as John mentions in the comment, even though C# is case-sensitive, other .NET languages are not, so you might be able to serialize this to XML but there's no guarantee that someone else will be able to deserialize it into their environment.

Rethink your design, so it doesn't have classes that look like this.



来源:https://stackoverflow.com/questions/2451997/is-xml-serialization-in-net-case-sensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!