c# - Applying normalization to xs:token during deserialization -
i have xml schema defines simple type based on xs:token, maximum length restriction.
when validate xml document against schema, validation correctly applies normalization content. contiguous whitespace characters replaced single space. e.g. "a b" normalized "a b" before maximum length restriction checked.
however, when deserialize xml document types generated xsd.exe, normalization not applied. can result in strings longer schema allows.
for reference, i'm using c# , .net 4.5.2.
here's minimal example demonstrate issue.
example xml schema:
<?xml version="1.0" encoding="utf-8"?> <xs:schema targetnamespace="http://tempuri.org/xmlschema.xsd" elementformdefault="qualified" xmlns="http://tempuri.org/xmlschema.xsd" xmlns:mstns="http://tempuri.org/xmlschema.xsd" xmlns:xs="http://www.w3.org/2001/xmlschema" > <xs:element name="testelement" type="testtype"/> <xs:complextype name="testtype"> <xs:sequence> <xs:element name="name" type="shorttoken"/> </xs:sequence> </xs:complextype> <xs:simpletype name="shorttoken"> <xs:restriction base="xs:token"> <xs:maxlength value="5"/> </xs:restriction> </xs:simpletype> </xs:schema> the type generated giving schema xsd.exe:
[system.codedom.compiler.generatedcodeattribute("xsd", "4.0.30319.18020")] [system.serializableattribute()] [system.diagnostics.debuggerstepthroughattribute()] [system.componentmodel.designercategoryattribute("code")] [system.xml.serialization.xmltypeattribute(namespace="http://tempuri.org/xmlschema.xsd")] [system.xml.serialization.xmlrootattribute("testelement", namespace="http://tempuri.org/xmlschema.xsd", isnullable=false)] public partial class testtype { private string namefield; /// <remarks/> [system.xml.serialization.xmlelementattribute(datatype="token")] public string name { { return this.namefield; } set { this.namefield = value; } } } a valid xml document according schema:
<?xml version="1.0" encoding="utf-8"?> <testelement xmlns="http://tempuri.org/xmlschema.xsd"> <name>a b</name> </testelement> if validate document, value of name element correctly normalized , no errors.
however, if use following code deserialize xml document, value of name not normalized.
xmlserializer xmlserialiser = new xmlserializer(typeof(testtype)); testtype result = (testtype)xmlserialiser.deserialize(xmlreader); it seem responsibility normalizing value lies xmlreader, need aware of schema. have tried using xmlreadersettings follows, without success.
xmlreadersettings settings = new xmlreadersettings(); settings.validationtype = validationtype.schema; settings.schemas.add(xmlschema); please provide me example of how setup xmlreader or xmlserializer such resulting value of "name" property "a b" rather "a b".
thanks!
Comments
Post a Comment