views:

246

answers:

1

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.

+4  A: 

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.

Aaronaught
I suppose I should also point out that if you're following Microsoft's C#/.NET coding conventions, then properties will all be in PascalCase anyway, so this situation shouldn't come up at all; a public class member that starts with a lowercase character goes against the convention.
Aaronaught
See clarification in edited post. So, is XML Serialization in .Net case sensitive or not?
logicnp
logicnp: The first line of the post says that the serializer preserves case and should work with your class just fine.
Gabe
@logicnp: The term "case sensitive" applies to how something is *read* or *parsed*. XML serialization is case *preserving* on the output, as the first paragraph states, which means that the case in the serialized XML will be identical to the case in C# and you will end up with two different XML elements, one for each field. Still, even if you just inherited this code and didn't write it, I would fix the class before trying to do anything fancy with it like serialization; Visual Studio can make short work of this and automate the rename across an entire solution for about 5 seconds of work.
Aaronaught