System.Xml.XmlSchemaIntegrityValidationWarning Class
The XmlSchemaIntegrityValidationWarning class represents a warning that occurs when an XML document validates against an XML schema with integrity checks enabled. It provides details about the warning, including the line number, position, and a descriptive message.
Namespace
System.Xml
Assembly
System.Xml.dll
Inheritance
Object → XmlSchemaIntegrityValidationWarning
Properties
- Message –
string– The warning message. - LineNumber –
int– The line number where the warning occurred. - LinePosition –
int– The character position in the line. - SourceUri –
string– The URI of the XML source.
Methods
- ToString() – Returns a string representation of the warning.
Example
using System;
using System.Xml;
using System.Xml.Schema;
class Program
{
static void Main()
{
string xml = @"<book isbn='1234567890'><title>Sample</title></book>";
string xsd = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='book'>
<xs:complexType>
<xs:sequence>
<xs:element name='title' type='xs:string'/>
</xs:sequence>
<xs:attribute name='isbn' type='xs:string' use='required'/>
</xs:complexType>
</xs:element>
</xs:schema>";
XmlSchemaSet schemas = new XmlSchemaSet();
schemas.Add("", XmlReader.Create(new System.IO.StringReader(xsd)));
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas = schemas;
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += (sender, e) =>
{
if (e.Severity == XmlSeverityType.Warning)
{
var warning = new XmlSchemaIntegrityValidationWarning
{
Message = e.Message,
LineNumber = e.Exception.LineNumber,
LinePosition = e.Exception.LinePosition,
SourceUri = e.Exception.SourceUri
};
Console.WriteLine(warning);
}
};
using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(xml), settings))
{
while (reader.Read()) ;
}
}
}