public void XsdAnyToSkipAttributeValidation () { // bug #358408 XmlSchemaSet schemas = new XmlSchemaSet (); schemas.Add (null, "Test/XmlFiles/xsd/358408.xsd"); XmlSchemaValidator v = new XmlSchemaValidator ( new NameTable (), schemas, new XmlNamespaceManager (new NameTable ()), XmlSchemaValidationFlags.ProcessIdentityConstraints); v.Initialize (); v.ValidateWhitespace (" "); XmlSchemaInfo info = new XmlSchemaInfo (); ArrayList list = new ArrayList (); v.ValidateElement ("configuration", "", info, null, null, null, null); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateWhitespace (" "); v.ValidateElement ("host", "", info, null, null, null, null); v.ValidateAttribute ("auto-start", "", "true", info); list.Clear (); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateEndElement (null);//info); v.ValidateWhitespace (" "); v.ValidateElement ("service-managers", "", info, null, null, null, null); list.Clear (); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateWhitespace (" "); v.ValidateElement ("service-manager", "", info, null, null, null, null); list.Clear (); v.GetUnspecifiedDefaultAttributes (list); v.ValidateEndOfAttributes (info); v.ValidateWhitespace (" "); v.ValidateElement ("foo", "", info, null, null, null, null); v.ValidateAttribute ("bar", "", "", info); }
public void XmlSchemaValidatorDoesNotEnforceIdentityConstraintsOnDefaultAttributesInSomeCases() { Initialize(); string xml = @"<?xml version='1.0'?> <root xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='idF016.xsd'> <uid val='test'/> <uid/></root>"; string xsd = @"<?xml version='1.0'?> <xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' elementFormDefault='qualified'> <xsd:element name='root'> <xsd:complexType> <xsd:sequence> <xsd:element ref='uid' maxOccurs='unbounded'/> </xsd:sequence> </xsd:complexType> <xsd:unique id='foo123' name='uuid'> <xsd:selector xpath='.//uid'/> <xsd:field xpath='@val'/> </xsd:unique> </xsd:element> <xsd:element name='uid' nillable='true'> <xsd:complexType> <xsd:attribute name='val' type='xsd:string' default='test'/> </xsd:complexType> </xsd:element> </xsd:schema>"; XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable()); XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add(null, XmlReader.Create(new StringReader(xsd))); schemas.Compile(); XmlSchemaValidationFlags validationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.AllowXmlAttributes; XmlSchemaValidator validator = new XmlSchemaValidator(namespaceManager.NameTable, schemas, namespaceManager, validationFlags); validator.Initialize(); using (XmlReader r = XmlReader.Create(new StringReader(xsd))) { while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: namespaceManager.PushScope(); if (r.MoveToFirstAttribute()) { do { if (r.NamespaceURI == "http://www.w3.org/2000/xmlns/") { namespaceManager.AddNamespace(r.LocalName, r.Value); } } while (r.MoveToNextAttribute()); r.MoveToElement(); } validator.ValidateElement(r.LocalName, r.NamespaceURI, null, null, null, null, null); if (r.MoveToFirstAttribute()) { do { if (r.NamespaceURI != "http://www.w3.org/2000/xmlns/") { validator.ValidateAttribute(r.LocalName, r.NamespaceURI, r.Value, null); } } while (r.MoveToNextAttribute()); r.MoveToElement(); } validator.ValidateEndOfAttributes(null); if (r.IsEmptyElement) goto case XmlNodeType.EndElement; break; case XmlNodeType.EndElement: validator.ValidateEndElement(null); namespaceManager.PopScope(); break; case XmlNodeType.Text: validator.ValidateText(r.Value); break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: validator.ValidateWhitespace(r.Value); break; default: break; } } validator.EndValidation(); } XmlReaderSettings rs = new XmlReaderSettings(); rs.ValidationType = ValidationType.Schema; rs.Schemas.Add(null, XmlReader.Create(new StringReader(xsd))); using (XmlReader r = XmlReader.Create(new StringReader(xml), rs)) { try { while (r.Read()) ; } catch (XmlSchemaValidationException e) { _output.WriteLine(e.Message); return; } } Assert.True(false); }
public void ValidateWithXmlReader(XmlSchemaSet schemas, string xml, string xsd) { XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable()); XmlSchemaValidationFlags validationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints | XmlSchemaValidationFlags.AllowXmlAttributes; XmlSchemaValidator validator = new XmlSchemaValidator(namespaceManager.NameTable, schemas, namespaceManager, validationFlags); validator.Initialize(); using (XmlReader r = XmlReader.Create(xsd)) { while (r.Read()) { switch (r.NodeType) { case XmlNodeType.Element: namespaceManager.PushScope(); if (r.MoveToFirstAttribute()) { do { if (r.NamespaceURI == "http://www.w3.org/2000/xmlns/") { namespaceManager.AddNamespace(r.LocalName, r.Value); } } while (r.MoveToNextAttribute()); r.MoveToElement(); } validator.ValidateElement(r.LocalName, r.NamespaceURI, null, null, null, null, null); if (r.MoveToFirstAttribute()) { do { if (r.NamespaceURI != "http://www.w3.org/2000/xmlns/") { validator.ValidateAttribute(r.LocalName, r.NamespaceURI, r.Value, null); } } while (r.MoveToNextAttribute()); r.MoveToElement(); } validator.ValidateEndOfAttributes(null); if (r.IsEmptyElement) goto case XmlNodeType.EndElement; break; case XmlNodeType.EndElement: validator.ValidateEndElement(null); namespaceManager.PopScope(); break; case XmlNodeType.Text: validator.ValidateText(r.Value); break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: validator.ValidateWhitespace(r.Value); break; default: break; } } validator.EndValidation(); } XmlReaderSettings rs = new XmlReaderSettings(); rs.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); rs.ValidationType = ValidationType.Schema; rs.Schemas.XmlResolver = new XmlUrlResolver(); rs.Schemas.Add(null, XmlReader.Create(xsd)); using (XmlReader r = XmlReader.Create(xml, rs)) { while (r.Read()) ; } Assert.Equal(warningCount, 0); Assert.Equal(errorCount, 0); }