views:

56

answers:

2

The XML schema I'm working with has specific character lengths for strings. So I may have a string that's like:

"Jim & Mary"

which is 10 chars in C# but when it is written to xml it becomes:

"Jim & Mary"

If an XML schema said the string could only be a max of 10 chars. Would this string pass?

If not, how can I test in C# for the string length as it would appear in XML?

Thanks for any help.

+1  A: 
System.Xml.Linq.XElement xet = new System.Xml.Linq.XElement("test", "jim & mary");
Console.WriteLine(xet.ToString());
Console.WriteLine(xet.Value);
Console.ReadLine();

Will output

<test>jim &amp; mary</test>
jim & mary

So &amp; counts as 1 character.

Matt Ellen
+2  A: 

I reach the same conclusion as Matt (&amp; is a single character), but based on the standards. The XML standard states that before validation, attribute values must be normalized, which includes entity replacement.

Also see the Treatment of Entities section, which states that they would be "Included" whether they were part of content or inside attribute values. "Included" has this meaning: "its replacement text is retrieved and processed, in place of the reference itself, as though it were part of the document at the location the reference was recognized".

Stephen Cleary
OK, that's good to hear. Thanks.