public string[] Validate(string xml, XmlSchemaSet schemas) {_validationErrors.Clear();XmlReaderSettings xsettings = new XmlReaderSettings(); xsettings.Schemas.Add(schemas);xsettings.ValidationType = ValidationType.Schema; xsettings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints;xsettings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;xsettings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; xsettings.ValidationEventHandler += XmlValidationEventHandler;using (StringReader sreader = new StringReader(xml)) {using (XmlReader xreader = XmlReader.Create(sreader, xsettings)) {while (xreader.Read()) ; }}return _validationErrors.ToArray(); } void XmlValidationEventHandler(object sender, ValidationEventArgs e) {if (e.Severity == XmlSeverityType.Error) {_validationErrors.Add(e.Message);}}
<?xml version="1.0" encoding="utf-8"?> <Envelope><Client LastName="Williams"> </Client> <Client LastName="Irwin"> </Client></Envelope>
<?xml version="1.0" encoding="utf-8" ?> <xsd:schema id="CRMExport" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:dt="http://distribution-technology.com" targetNamespace="http://distribution-technology.com" > <xsd:element name="Envelope"><xsd:complexType> <xsd:sequence><xsd:element minOccurs="1" maxOccurs="2" name="Client"> <xsd:complexType><xsd:attribute name="FirstName" type="xsd:string" use="required" /> <xsd:attribute name="LastName" type="xsd:string" use="required" /></xsd:complexType> </xsd:element></xsd:sequence> </xsd:complexType></xsd:element> </xsd:schema>