/// <summary> /// Validates the XML per XSD. /// </summary> /// <param name="xmlString">The XML string.</param> /// <param name="xsdFile">The XSD file.</param> /// <returns></returns> public static XMLValidateResult ValidateXMLPerXSD(string xmlString, string xsdFile) { xmlString = xmlString.Replace("<tns:path xsi:nil=\"true\"/>", ""); xmlString = xmlString.Replace("<tns:seriesInstanceUid xsi:nil=\"true\"/>", ""); xmlString = xmlString.Replace("<tns:seriesDate xsi:nil=\"true\"/>", ""); //xmlString = xmlString.Replace("<tns:tags/>", "<tns:tags>noarchived</tns:tags>"); XMLValidateResult validateResult = new XMLValidateResult(); validateResult.isValid = true; validateResult.message = string.Empty; XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.Schemas.Add("http://www.carestreamhealth.com/CSI/CSDM/1/Schema", xsdFile); // Note: if xsd namespace changes, need change this accordingly readerSettings.ValidationType = ValidationType.Schema; readerSettings.ValidationEventHandler += new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Error) { validateResult.isValid = false; } validateResult.message = e.Message; }); MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)); mStream.Position = 0; XmlReader xmlReader = XmlReader.Create(mStream, readerSettings); while (xmlReader.Read()) { if (validateResult.isValid == false) // there is node not valid, stop the check { break; } } xmlReader.Close(); return(validateResult); }
/// <summary> /// Validates the XML per XSD. /// </summary> /// <param name="xmlString">The XML string.</param> /// <param name="xsdFile">The XSD file.</param> /// <returns></returns> public static XMLValidateResult ValidateXMLPerXSD(string xmlString, string xsdFile) { xmlString = xmlString.Replace("<tns:path xsi:nil=\"true\"/>", ""); xmlString = xmlString.Replace("<tns:seriesInstanceUid xsi:nil=\"true\"/>", ""); xmlString = xmlString.Replace("<tns:seriesDate xsi:nil=\"true\"/>", ""); //xmlString = xmlString.Replace("<tns:tags/>", "<tns:tags>noarchived</tns:tags>"); XMLValidateResult validateResult = new XMLValidateResult(); validateResult.isValid = true; validateResult.message = string.Empty; XmlReaderSettings readerSettings = new XmlReaderSettings(); readerSettings.Schemas.Add("http://www.carestreamhealth.com/CSI/CSDM/1/Schema", xsdFile); // Note: if xsd namespace changes, need change this accordingly readerSettings.ValidationType = ValidationType.Schema; readerSettings.ValidationEventHandler += new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Error) { validateResult.isValid = false; } validateResult.message = e.Message; }); MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)); mStream.Position = 0; XmlReader xmlReader = XmlReader.Create(mStream, readerSettings); while (xmlReader.Read()) { if (validateResult.isValid == false) // there is node not valid, stop the check { break; } } xmlReader.Close(); return validateResult; }