code_junkie

How force a maximum string length in a C# web service object property?

Nov 11th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. public class Person
  2. {
  3. public string FirstName { get; set; }
  4. public string LastName { get; set; }
  5. }
  6.  
  7. [ValidationSchema("person.xsd")]
  8. public class Person { /* ... */ }
  9.  
  10. <!-- person.xsd -->
  11.  
  12. <?xml version="1.0"?>
  13. <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  14.  
  15. <xsd:element name="Person" type="PersonType" />
  16.  
  17. <xsd:simpleType name="NameString">
  18. <xsd:restriction base="xsd:string">
  19. <xsd:maxLength value="255"/>
  20. </xsd:restriction>
  21. </xsd:simpleType>
  22.  
  23. <xsd:complexType name="PersonType">
  24. <xsd:sequence>
  25. <xsd:element name="FirstName" type="NameString" maxOccurs="1"/>
  26. <xsd:element name="LastName" type="NameString" maxOccurs="1"/>
  27. </xsd:sequence>
  28. </xsd:complexType>
  29. </xsd:schema>
  30.  
  31. using System.ComponentModel.DataAnnotations;
  32. public class Person
  33. {
  34. [StringLength(255, ErrorMessage = "Error")]
  35. public string FirstName { get; set; }
  36. [StringLength(255, ErrorMessage = "Error")]
  37. public string LastName { get; set; }
  38. }
Add Comment
Please, Sign In to add comment