런타임에 xml 파일을 읽었습니다. xml을 런타임에 xsd 파일과 비교하여 유효성을 검사 할 수 있습니까? C#을에게런타임시 코드를 사용하여 xsd에 대해 xml의 유효성을 검사 할 수 있습니까?
답변
이 시도 :
public void ValidateXmlDocument(
XmlReader documentToValidate, string schemaPath)
{
XmlSchema schema;
using (var schemaReader = XmlReader.Create(schemaPath))
{
schema = XmlSchema.Read(schemaReader, ValidationEventHandler);
}
var schemas = new XmlSchemaSet();
schemas.Add(schema);
var settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = schemas;
settings.ValidationFlags =
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += ValidationEventHandler;
using (var validationReader = XmlReader.Create(documentToValidate, settings))
{
while (validationReader.Read())
{
}
}
}
private static void ValidationEventHandler(
object sender, ValidationEventArgs args)
{
if (args.Severity == XmlSeverityType.Error)
{
throw args.Exception;
}
Debug.WriteLine(args.Message);
}
희망을 사용하여이 링크는 도움 :
링크가 괜찮은 외모,하지만 난 예를 쓸모있는 XmlValidatingReader를를 사용났습니다. 비헤이비어를 정의하는 Validation 속성이 포함 된 XmlReader 및 XmlReaderSettings를 살펴보십시오. – STW
내가 CODE TOO있어! 내 테스트에서 이것을 사용
public static bool IsValid(XElement element, params string[] schemas)
{
XmlSchemaSet xsd = new XmlSchemaSet();
XmlReader xr = null;
foreach (string s in schemas)
{ // eh, leak 'em.
xr = XmlReader.Create(
new MemoryStream(Encoding.Default.GetBytes(s)));
xsd.Add(null, xr);
}
XDocument doc = new XDocument(element);
var errored = false;
doc.Validate(xsd, (o, e) => errored = true);
if (errored)
return false;
// If this doesn't fail, there's an issue with the XSD.
XNamespace xn = XNamespace.Get(
element.GetDefaultNamespace().NamespaceName);
XElement fail = new XElement(xn + "omgwtflolj/k");
fail.SetAttributeValue("xmlns", xn.NamespaceName);
doc = new XDocument(fail);
var fired = false;
doc.Validate(xsd, (o, e) => fired = true);
return fired;
}
이 사람은 (어셈블리 내에서 파일 리소스)를 문자열로 스키마에 소요 스키마 세트에 추가합니다. 유효성을 검사하고 유효하지 않으면 false를 반환합니다.
xml이 유효하지 않은 것으로 확인되지 않으면 부정적 체크를 수행하여 스키마가 엉망이되지 않도록하십시오. 그것의 절대 보장하지,하지만이 내 스키마에서 오류를 찾기 위해 사용했습니다.
'XmlSchema.Read' 메쏘드는 구문 분석하는 동안 스키마의 유효성을 검증 할 수 있기 때문에 사용하는 것이 좋습니다. –
코드 주셔서 감사합니다 :) – BigOmar
간단한 해결책 ..
try
{
XmlReaderSettings Xsettings = new XmlReaderSettings();
Xsettings.Schemas.Add(null, "personDivideSchema.xsd");
Xsettings.ValidationType = ValidationType.Schema;
XmlDocument document = new XmlDocument();
document.Load("person.xml");
XmlReader reader = XmlReader.Create(new StringReader(document.InnerXml), Xsettings);
while (reader.Read());
}
catch (Exception e)
{
Console.WriteLine(e.Message.ToString());
}
- 1. Silverlight 3에서 XSD에 대해 XML의 유효성을 검사 할 수 있습니까?
- 2. 이 xsd에 대해이 xml의 유효성을 검사 할 수없는 이유는 무엇입니까?
- 3. PHP에서 여러 스키마에 대해 XML의 유효성을 검사 할 수 있습니까?
- 4. xsd에 대해 xml 유효성 검사 수행
- 5. XSD에 대해 XML의 유효성을 검사하는 동안 요소 순서 무시
- 6. xsd에 대해 xml의 유효성을 검사 할 수 없습니다. 오류 "cvc-elt.1 : '시스템'요소의 선언을 찾을 수 없습니다."
- 7. URL 내가이 XML의 유효성을 검사 할 때
- 8. C# XML의 유효성을 검사 할 때 스키마 정보 가져 오기
- 9. javascript에서 xsd에 대해 xml 유효성을 검사하는 교차 플랫폼 방식이 있습니까?
- 10. SAX를 사용하여 XML의 유효성을 검사하는 방법?
- 11. 고유 한 가져 오기 및 네임 스페이스가있는 XSD에 대해 XML의 유효성을 검사하려면 어떻게해야합니까?
- 12. C++을 사용하여 xsd에 대해 xml 유효성 확인
- 13. .NET에서 XSD에 대해 유효성을 검사 할 때 전체 XML BLOB가 메모리에 저장됩니까?
- 14. C++에서 XSD에 대해 XML 노드의 유효성을 검증하는 방법은 무엇입니까?
- 15. SOAP UI : 로컬 XML 스키마 파일을 사용하여 유효성을 검사 할 표명을 추가 할 수 있습니까?
- 16. 이 xsd에 어떤 문제가 있습니까?
- 17. WCF 웹 서비스에서 x509 ClientCertificate의 유효성을 검사 할 수 있습니까?
- 18. 리플렉션을 사용하여 메소드의 코드를 검사 할 수 있습니까?
- 19. 사용자 지정 메서드에서 유효성을 검사 할 수 있습니까?
- 20. CLASSPATH에서 XSD를 참조하여 XML 유효성을 검사 할 수 있습니까?
- 21. HttpUtility.Html 사용자 항목의 유효성을 검사 할 코드를 입력하십시오.
- 22. java.net.ConnectException : XSD에 대한 XSD 유효성 검사 : 로컬 컴퓨터
- 23. 로컬 XSD 파일없이 XML 스키마에 대해 XML의 유효성을 검사하는 방법
- 24. PHP를 사용하여 Schematron에 대해 XML 유효성 검사
- 25. Webservices Design 및 XSD에 대해 유효성을 검사 할 수 있도록 C# .net에서 webservice 호출로 전송 된 원시 XML을 캡처 할 수 있습니까?
- 26. <marquee>의 유효성을 검사 할 수 있습니까?
- 27. bassassistance의 jquery 유효성 검사 플러그인을 사용하여 개별적으로 필드 세트의 유효성을 검사 할 수 있습니까?
- 28. xwork 유효성 검사 프레임 워크를 사용하여 서비스 계층의 유효성을 검사 할 수 있습니까
- 29. ActionScript의 XML 스키마에 대해 XML의 유효성을 테스트하는 방법은 무엇입니까?
- 30. 런타임시 .NET 힙 검사
이것이 가장 빠른 것입니다. –