2011-01-10 3 views
5

C# 및 .net 3.5를 사용하여 포함하는 스키마에 대해 XML 문서의 유효성을 검사하려고합니다. > xmldocument 및 중첩 스키마

을 another.xsd 포함 another.xsd - -

스키마를 거기에 포함이

Schema1.xsd은 다음과 같습니다> 등 내가 Schema1을 추가하려고하면

base.xsd. xsd XmlDocument 다음 오류가 발생합니다.

'YesNoType'유형이 선언되지 않았거나 단순 유형이 아닙니다.

Schema1.xsd 스키마를로드 할 때 base.xsd 파일이 포함되어 있지 않기 때문에이 오류가 발생한다고 생각합니다.

XmlSchemaSet 클래스를 사용하려고하는데 XmlResolver URI를 스키마 위치로 설정합니다.

참고 : 모든 스키마가 같은 디렉토리 E에서 살고 : \ 데브 \ 홈페이지 \ XmlSchemas를

여기에 코드를입니다

string schemaPath = "E:\\Dev\\Main\\XmlSchemas"; 

XmlDocument xmlDocSchema = new XmlDocument(); 

XmlSchemaSet s = new XmlSchemaSet(); 

XmlUrlResolver resolver = new XmlUrlResolver(); 

Uri baseUri = new Uri(schemaPath); 

resolver.ResolveUri(null, schemaPath); 

s.XmlResolver = resolver; 

s.Add(null, XmlReader.Create(new System.IO.StreamReader(schemaPath + "\\Schema1.xsd"), new XmlReaderSettings { ValidationType = ValidationType.Schema, XmlResolver = resolver }, new Uri(schemaPath).ToString())); 


xmlDocSchema.Schemas.Add(s); 

ValidationEventHandler valEventHandler = new ValidationEventHandler 
(ValidateNinoDobEvent); 

try 
{ 
xmlDocSchema.LoadXml(xml); 
xmlDocSchema.Validate(valEventHandler); 
} 
catch (XmlSchemaValidationException xmlValidationError) 
{ 
// need to interogate the Validation Exception, for possible further 
// processing. 
string message = xmlValidationError.Message; 
return false; 
} 

사람이에 대해 XmlDocument를 검증에 대한 올바른 방향으로 날 지점 수 중첩 된 포함 스키마. 내가 무엇을 당신이해야 할 것은 스키마를 병합하는 것입니다 생각

+0

현재로서는 C# .NET과 같은 것이 없습니다. –

답변

0

: 그들은 당신이 계층 구조의 맨 아래에서 시작해야한다는 것을 의미하는 중첩 된 경우

http://asp.dotnetheaven.com/howto/doc/Xml/MultipleSchemas.aspx

그 순서대로로드하십시오. 나는 찾을 수있는 샘플에 엄밀히 말해서 중첩 된 구조가 아니라 오히려 보완적인 구조이기 때문에 100 % 확실하지 않습니다. 행운을 빕니다.

1

또한 중첩 된 스키마 케이스가 있는데 유효성 검사에 오류가 없습니다. 내 코드는 다음과 같습니다.

private string strLogger = null; 
    public bool ValidateXml(string path2XMLFile, string path2XSDFile) 
    { 
     bool isValidFile = false; 
     try 
     { 
      XmlReaderSettings settings = new XmlReaderSettings(); 
      settings.ValidationType = ValidationType.Schema; 
      settings.Schemas.Add(null, path2XSDFile); 
      settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); 
      XmlReader reader = XmlReader.Create(path2XMLFile, settings); 
      while (reader.Read()) ; 
      if (String.IsNullOrEmpty(strLogger)) 
      { 
       isValidFile = true; 
      }     
     } 
     catch (Exception ex) 
     { 
      LoggingHandler.Log(ex); 
     } 
     return isValidFile; 
    } 
    private void settings_ValidationEventHandler(object sender, ValidationEventArgs e) 
    { 
     strLogger += System.Environment.NewLine + "Validation Error Message = [" + e.Message + "], " + "Validation Error Severity = [" + e.Severity + "], " + System.Environment.NewLine; 
    }