I’m reading some XML with XmlReader using the following code:
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;
settings.Schemas.Add(xmlSchemaSet);
using (var reader = XmlReader.Create(new StringReader(formatXml), settings))
{
while (reader.Read())
{
...
}
}
And for each element, I read all the attributes like this:
while (reader.MoveToNextAttribute())
{
...
}
However, this code does not pick up attributes that have default values specified in the XSD schema like this:
<xs:attribute name="new" type="xs:string" default="error" />
It only picks up those attributes that are explicitly set in the XML. How do I get it to automatically get the default values from the XSD as well?
Turned out everything was working the way it should, and the default attributes were read. However there were some validation errors that I had overlooked, which resulted in the attributes not being set.
So the answer would be: Make sure your XML validate before default attributes work.