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.

logicnp
  • 5,796
  • 1
  • 28
  • 32
  • 2
    The bigger problem here is that you have public fields - not to mention a non-public class, which the XML serializer won't like. – Aaronaught Mar 16 '10 at 03:47
  • Yes, and VB.NET programs won't like you very well, either. Neither will anyone who has to maintain that code. Solution: rename one of those fields. – John Saunders Mar 16 '10 at 03:49
  • See clarification in edited post. – logicnp Mar 16 '10 at 04:07
  • So, is XML Serialization in .Net case sensitive or not? – logicnp Mar 16 '10 at 04:10
  • related: **Do not use public Fields**: http://msdn.microsoft.com/en-us/library/ms229057.aspx Public and protected fields do not version well and are not protected by code access security demands. Instead of using publicly visible fields, use private fields and expose them through properties. – Cheeso Mar 17 '10 at 14:32
  • See http://stackoverflow.com/questions/7414747/is-xml-case-sensitive "Is Xml Case Sensitive" Xml is case sensitive. –  Sep 29 '14 at 14:16

1 Answers1

4

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
  • 120,909
  • 25
  • 266
  • 342
  • 1
    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 Mar 16 '10 at 04:04
  • See clarification in edited post. So, is XML Serialization in .Net case sensitive or not? – logicnp Mar 16 '10 at 04:11
  • logicnp: The first line of the post says that the serializer preserves case and should work with your class just fine. – Gabe Mar 16 '10 at 04:18
  • 1
    @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 Mar 16 '10 at 04:26