2015-01-27 3 views
1

this과 같은 xsd 파일을 만들려고합니다.XML 스키마에서 중첩 된 XmlSchemaComplexType 만들기

나는이 잘 작동하지 않습니다이

  XmlSchema schema = new XmlSchema(); 
      schema.Id = "SHP_CAHPS_HOSPICE_DATA"; 

      XmlSchemaElement elementData = new XmlSchemaElement(); 
      schema.Items.Add(elementData); 
      elementData.Name = "SHP_CAHPS_HOSPICE_DATA"; 


      XmlSchemaComplexType complexType = new XmlSchemaComplexType(); 
      elementData.SchemaType = complexType;// This is same as below. It's working fine but next is not working. 

      XmlSchemaSequence sequence = new XmlSchemaSequence(); 
      complexType.Particle = sequence; 

      // here some elements 

      XmlSchemaElement surveydataEle = new XmlSchemaElement(); 
      sequence.Items.Add(surveydataEle); 
      surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA"; 
      surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); 
      surveydataEle.MinOccurs = 1; 
      surveydataEle.MaxOccurs = 2000; 

      XmlSchemaComplexType complexType1 = new XmlSchemaComplexType(); 
      surveydataEle.SchemaType = complexType1; 

      XmlSchemaSequence sequence1 = new XmlSchemaSequence(); 
      complexType1.Particle = sequence1; 

      Type type = patient.GetType(); 
      PropertyInfo[] properties = type.GetProperties(); 
      foreach (PropertyInfo property in properties) 
      { 
       string name = property.Name; 
       XmlSchemaElement element = new XmlSchemaElement(); 
       sequence1.Items.Add(element); 
       element.Name = name; 
       element.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); 
       element.MinOccurs = 0; 
      } 

      XmlSchemaSet schemaSet = new XmlSchemaSet(); 
      schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallbackOne); 
      schemaSet.Add(schema); 
      schemaSet.Compile(); 

      XmlSchema compiledSchema = null; 

      foreach (XmlSchema schema1 in schemaSet.Schemas()) 
      { 
       compiledSchema = schema1; 
      } 
      Random r = new Random(); 
      int random = r.Next(); 
      string filePath = "D:\\" + random + ".xsd"; 
      if (System.IO.File.Exists(filePath)) 
      { 
       System.IO.File.Delete(filePath); 
      } 
      //Stream stream = new MemoryStream(); 
      XmlTextWriter writer = new XmlTextWriter(filePath, new UTF8Encoding()); 
      XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable()); 
      nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema"); 
      compiledSchema.Write(writer, nsmgr); // Exception is occurring here 
      writer.Flush(); 
      writer.Close(); 

을 사용하고 있습니다. 내가 두 번째 XmlSchemaComplexType 다음 가 작동하고 벌금을 사용하고 있지 않다 때 는 예외 Object reference not set to an instance of an objectcompiledSchema.Write(writer, nsmgr);

에서 발생했습니다. 여기 같은 방법으로 동일한 주먹을 사용하고 있습니다 XmlSchemaComplexType을 추가하는 데 사용하고 있지만 그것이 작동하지 않는 두 번째 시간.

제발. compiledSchemaschemaSet.Schemas()이 스키마가 고장이기 때문이다 schemaSet.Add(schema) 실패 때문이다 빈 수집,이기 때문이다, 널 (null)이기 때문에

답변

1

NullReferenceException가 발생되고있다. 당신은 그것을 선언

<?xml version="1.0" encoding="utf-16"?> 
<xsd:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="SHP_CAHPS_HOSPICE_DATA" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="SHP_CAHPS_HOSPICE_DATA"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element minOccurs="1" maxOccurs="2000" name="CAHPS_HOSPICE_SURVEY_DATA" type="xsd:string"> 
        <xsd:complexType> 
         <xsd:sequence> 

:

스키마가 깨진 이유

하나가 당신의 schema 변수에서 XML을 덤핑으로 볼 수있는 것처럼, 문자열 및 복잡한 유형을 모두 할 CAHPS_HOSPICE_SURVEY_DATA를 선언하는 것입니다 여기에 문자열 SchemaTypeName를 사용 :

: 당신은 다음을 선언

 surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); 

여기에 복잡한 유형 SchemaType 사용하는 것으로

 XmlSchemaElement surveydataEle = new XmlSchemaElement(); 
     sequence.Items.Add(surveydataEle); 
     surveydataEle.Name = "CAHPS_HOSPICE_SURVEY_DATA"; 
     // surveydataEle.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema"); <-- Deleted this line. 
     surveydataEle.MinOccurs = 1; 
     surveydataEle.MaxOccurs = 2000; 

     XmlSchemaComplexType complexType1 = new XmlSchemaComplexType(); 
     surveydataEle.SchemaType = complexType1; 
:
 surveydataEle.SchemaType = complexType1; 

수정은 첫 번째 선언을 삭제하는 것입니다