2013-10-03 59 views
1

我想從給定的XSD中檢索屬性列表。我能夠獲得幾乎所有的屬性簡單和複雜的類型。但是我無法檢索那些爲其設置了擴展和複雜內容的模式元素的屬性。我用XmlSchemaComplexContent和XmlSchemaComplexContentExtension類在c#中嘗試了不同的東西,但可能是我沒有正確使用它們,任何人都可以請我提供關於如何使用這些類或適當的xsd解析教程的指南。如何解析複雜類型中具有複雜內容和擴展名的XSD?

我想提取這些序列元素。

<xs:element name="xyz" type="xyz"/> 
<xs:complexType name="xyz"> 
<xs:complexContent> 
    <xs:extension base="robj"> 
    <xs:sequence> 
     <xs:element name="xyzHierarchy" type="rnb_v1_2:NList" /> 
     <xs:element name="Attributes" type="xyzOptions" /> 
     <xs:element name="Country" type="riD" /> 
     <xs:element name="CustomFields" /> 
     <xs:element name="DisplayName" type="xs:string" /> 
     <xs:element name="DisplayOrder" type="xs:int"/> 
      <xs:element name="Signature" type="xs:string" nillable="true"/> 

    </xs:extension> 
    </xs:complexContent> 

+0

你能請張貼給你提供問題的XSD片段,以及你想從中提取哪些信息清單? – MiMo

回答

1

我得到了答案。 我想與大家分享。 其中toFile是一個字符串類型 這裏我只寫了執行特定任務而不是整個代碼的代碼部分。 ///////////////////////////////////////////////// //////////////////////

private static void main(String[] args) 
if (complexType != null) 
        { 
         XmlSchemaParticle particle = complexType.Particle; 
         XmlSchemaSequence sequence = particle as XmlSchemaSequence; 
         toFile = toFile + "Complex " + complexType.Name; 
         toFile = toFile + "\n"; 
         if (complexType.ContentModel != null) 
         { 
          xmlComplexContent = (XmlSchemaComplexContent)complexType.ContentModel; 

          if (complexType != null) 
          { 
           extension = (XmlSchemaComplexContentExtension)xmlComplexContent.Content; 
           extension.BaseTypeName = complexType.QualifiedName; // base name 
           sequence =(XmlSchemaSequence) extension.Particle; 
           if (extension.BaseTypeName.Name != null) 
           { 

            if (sequence != null) 
            { 

             Out(extension.Particle); 
            } 
           } 

          } 
         } 

         else 
          Out(complexType.Particle); 
       } 
     System.IO.File.WriteAllText(@"C:\\Users\\KAMALPREETDEV\\Desktop\\attributes.txt", toFile); 

     } 

     private static void Out(XmlSchemaParticle particle) 
     { 
      XmlSchemaSequence sequence = particle as XmlSchemaSequence; 
      XmlSchemaChoice choice = particle as XmlSchemaChoice; 
      XmlSchemaAll all = particle as XmlSchemaAll; 

      if (sequence != null) 
      { 

       toFile = toFile + " Sequence"; 
       toFile = toFile + "\n"; 
       for (int i = 0; i < sequence.Items.Count; i++) 
       { 
        XmlSchemaElement childElement = sequence.Items[i] as XmlSchemaElement; 
        XmlSchemaSequence innerSequence = sequence.Items[i] as XmlSchemaSequence; 
        XmlSchemaChoice innerChoice = sequence.Items[i] as XmlSchemaChoice; 
        XmlSchemaAll innerAll = sequence.Items[i] as XmlSchemaAll; 

        if (childElement != null) 
        { 

         toFile = toFile + " Element/Type: " + childElement.Name + "/" + childElement.SchemaTypeName.Name; 
         toFile = toFile + "\n"; 
        } 
        else Out(sequence.Items[i] as XmlSchemaParticle); 
       } 
      }