c# - XSD validation not failing trailing newline -
xml validation not touch except when have to, there's stupid i'm missing , far i've been unsuccessful in googling help. issue have type restriction says can letters or spaces. element leading newline fails validation, trailing newline passes. how trailing newline fail?
i've created stripped down test case follows:
validation code:
public list<xsdvalidationerror> validatexmlagainstxsd(string xml, string xsdfilepath, boolean processschemalocation = false) { var ret = new list<xsdvalidationerror>(); var xss = new xmlschemaset(); var xmlurlresolver = new xmlurlresolver(); xmlurlresolver.cachepolicy = new requestcachepolicy(requestcachelevel.default); xss.xmlresolver = xmlurlresolver; var xsdxelement = xelement.parse(file.readalltext(xsdfilepath)); var targetnamespaceattribute = xsdxelement.attribute("targetnamespace"); xss.add(targetnamespaceattribute != null ? targetnamespaceattribute.value : "", xsdfilepath); var settings = new xmlreadersettings(); settings.validationtype = validationtype.schema; settings.schemas = xss; settings.validationflags = xmlschemavalidationflags.processinlineschema; if (processschemalocation) settings.validationflags |= xmlschemavalidationflags.processschemalocation; settings.validationeventhandler += (sender, e) => { var xve = new xsdvalidationerror { message = e.message, linenumber = e.exception.linenumber, lineposition = e.exception.lineposition }; ret.add(xve); }; using (var sr = new stringreader(xml)) { var xr = xmlreader.create(sr, settings); while (xr.read()); return ret; } } public class xsdvalidationerror { public string message { get; set; } public int linenumber { get; set; } public int lineposition { get; set; } public override string tostring() { return string.format("line {0:n0}, position {1:n0}: {2}", this.linenumber, this.lineposition, this.message); } }
input xml , xsd:
<people> <person>hello person 1 </person> <person>hello person two</person> <person> hello person three</person> </people> <xs:schema xmlns:xs="http://www.w3.org/2001/xmlschema"> <xs:element name="people"> <xs:complextype> <xs:sequence> <xs:element name="person" maxoccurs="unbounded"> <xs:simpletype> <xs:restriction base="xs:string"> <xs:pattern value="[a-za-z ]+"/> </xs:restriction> </xs:simpletype> </xs:element> </xs:sequence> </xs:complextype> </xs:element> </xs:schema>
in xml person 1 not fail, person 2 passes should, , person 3 fails should. need person 1 fail.
i cannot change input xml or xsd. visual studio correctly validates file. ideas?
i think quirk/bug microsoft's xsd parser. definition xs:string type :-
<xsd:simpletype name="string" id="string"> <xsd:restriction base="xsd:anysimpletype"> <xsd:whitespace value="preserve"/> </xsd:restriction> </xsd:simpletype>
as whitespace facet set 'preserve' should contain in element, whitespace , all.
however have noticed ignoring trailing whitespace. seem there not lot can other apply validation rule manually in code.
incidently validates expected in xerces (erroring both leading , trailing whitespace).
Comments
Post a Comment