2

我正在尝试从给定的 XSD 中检索属性列表。我能够获得几乎所有简单和复杂类型的属性。但是我无法检索那些设置了扩展和复杂内容的模式元素的属性。我在 C# 中使用 XmlSchemaComplexContent 和 XmlSchemaComplexContentExtension 类尝试了不同的东西,但可能是我没有正确使用它们,任何人都可以为我提供有关如何使用这些类的指南或一些适当的 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>

4

1 回答 1

1

我得到了答案。我想与大家分享。其中 toFile 是 String 类型,这里我只写了执行特定任务的部分代码,而不是整个代码。///////////////////////////////////////// //////////////////////////////////

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);
                }
            }
于 2013-10-04T08:57:50.150 回答