을 사용할 수 있습니다로드하려면이 코드
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("....", "youXsd.xsd");
settings.ValidationType = ValidationType.Schema;
settings.ValidationEventHandler += new ValidationEventHandler(YourSettingsValidationEventHandler);
XmlReader books = XmlReader.Create("YouFile.xml", settings);
while (books.Read()) { }
//Your validation
static void YourSettingsValidationEventHandler(object sender, ValidationEventArgs e)
{
}
2 시도 할 수 있습니다, 나는 Load the xsd file into a string
실제로 무엇을 의미하는지 잘 모른다, 그러나 멀리있다 XML을 검증하는 간단한 메소드.
var xDoc = XDocument.Load(xmlPath);
var set = new XmlSchemaSet();
using (var stream = new StreamReader(xsdPath))
{
// the null here is a validation call back for the XSD itself, unless you
// specifically want to handle XSD validation errors, I just pass a null and let
// an exception get thrown as there usually isn't much you can do with an error in
// the XSD itself
set.Add(XmlSchema.Read(stream, null));
}
xDoc.Validate(set, ValidationCallBack);
그런 다음 당신은 그냥 유효성 검사 오류에 대한 핸들러로 클래스에 ValidationCallBack
라는 방법이 필요합니다 (당신은 Validate()
방법은 위의이 방법을 참조하는 매개 변수 당신이 원하는대로 그것을 이름 만 위임 할 수 있습니다) :
를
public void ValidationCallBack(object sender, ValidationEventArgs e)
{
// do something with any errors
}
그래서 XML과 XSD 파일이 분리되어 있으며 XSD를 사용하여 XML의 유효성을 검사하겠습니까? – psubsee2003
@ psubsee2003 예, 내 xml 데이터가 서버에서 가져옵니다. –