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);
}