2009-04-24 7 views
4

.net을 사용하여 xsd 스키마 파일에서 요소의 열거 제약 조건 값을 프로그래밍 방식으로 추출하는 방법은 무엇입니까?.net의 xsd 스키마 파일에서 열거 값 추출

예를 들어, 나는 다음과 같은 XSD에서 '아우디', '골프'와 'BMW'추출하고 싶습니다 :

<xs:element name="car"> 
    <xs:simpleType> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Audi"/> 
     <xs:enumeration value="Golf"/> 
     <xs:enumeration value="BMW"/> 
    </xs:restriction> 
    </xs:simpleType> 
</xs:element> 

답변

4

XmlSchema 클래스가있다,하지만 꽤 ... "재미를 본다 "함께 일해라.

xml 쿼리로 충분합니까?

XmlDocument doc = new XmlDocument(); 
doc.Load("Foo.xsd");    
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable); 
mgr.AddNamespace("xx", "http://www.w3.org/2001/XMLSchema"); 
foreach (XmlElement el in doc.SelectNodes("//xx:element[@name='car'][1]/xx:simpleType/xx:restriction/xx:enumeration", mgr)) 
{ 
    Console.WriteLine(el.GetAttribute("value")); 
} 
+0

충분합니다. 감사. – pavliks

4

스키마의 모든 항목을 반복 할 수도 있습니다.

다음 코드는 XSD 스키마에서 데이터를 추출하는 데 매우 유용합니다. 기본적으로 SOM의 절대 객체에 대한 함수 콜백을 호출합니다. 형식이 열거 형인지 확인하고 그에 따라 처리하는 콜백 구현을 제공해야합니다.

 #region CallForEveryElement 
    public delegate void SchemaObjectFunction(XmlSchemaObject xso); 

    public static void CallForEveryElement(XmlSchema schema, SchemaObjectFunction function) 
    { 
     CallForEveryElementInternal(schema, function); 
    } 

    private static void CallForEveryElementInternal(XmlSchemaObject schemaObject, SchemaObjectFunction function) 
    { 
     if (schemaObject == null) 
      return; 

     function(schemaObject); 

     if (schemaObject is XmlSchema) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchema).Includes, function); 
      CallForEveryElementInternal((schemaObject as XmlSchema).Items, function); 
     } 
     if (schemaObject is XmlSchemaGroupBase) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaGroupBase).Items, function); 
     } 
     if (schemaObject is XmlSchemaAttribute) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaAttribute).SchemaType, function); 
     } 
     if (schemaObject is XmlSchemaAttributeGroup) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaAttributeGroup).AnyAttribute, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaAttributeGroup).Attributes, function); 
     } 
     if (schemaObject is XmlSchemaContentModel) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaContentModel).Content, function); 
     } 
     if (schemaObject is XmlSchemaComplexContentExtension) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentExtension).AnyAttribute, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentExtension).Attributes, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentExtension).Particle, function); 
     } 
     if (schemaObject is XmlSchemaComplexContentRestriction) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentRestriction).AnyAttribute, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentRestriction).Attributes, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentRestriction).Particle, function); 
     } 
     if (schemaObject is XmlSchemaComplexType) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).AnyAttribute, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).Attributes, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).AttributeWildcard, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).ContentModel, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).ContentTypeParticle, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).Particle, function); 
     } 
     if (schemaObject is XmlSchemaElement) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaElement).Constraints, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaElement).ElementSchemaType, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaElement).SchemaType, function); 
     } 
     if (schemaObject is XmlSchemaGroup) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaGroup).Particle, function); 
     } 
     if (schemaObject is XmlSchemaGroupBase) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaGroupBase).Items, function); 
     } 
     if (schemaObject is XmlSchemaGroupRef) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaGroupRef).Particle, function); 
     } 
     if (schemaObject is XmlSchemaImport) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaImport).Annotation, function); 
     } 
     if (schemaObject is XmlSchemaInclude) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaInclude).Annotation, function); 
     } 
     if (schemaObject is XmlSchemaRedefine) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaRedefine).Items, function); 
     } 
     if (schemaObject is XmlSchemaSimpleContentExtension) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentExtension).AnyAttribute, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentExtension).Attributes, function); 
     } 
     if (schemaObject is XmlSchemaSimpleContentRestriction) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).AnyAttribute, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).Attributes, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).BaseType, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).Facets, function); 
     } 
     if (schemaObject is XmlSchemaSimpleType) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleType).Content, function); 
     } 
     if (schemaObject is XmlSchemaSimpleTypeList) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeList).BaseItemType, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeList).ItemType, function); 
     } 
     if (schemaObject is XmlSchemaSimpleTypeRestriction) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeRestriction).BaseType, function); 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeRestriction).Facets, function); 
     } 
     if (schemaObject is XmlSchemaSimpleTypeUnion) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeUnion).BaseTypes, function); 
     } 
     if (schemaObject is XmlSchemaType) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaType).BaseXmlSchemaType, function); 
     } 
     if (schemaObject is XmlSchemaAnnotated) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaAnnotated).Annotation, function); 
     } 
     if (schemaObject is XmlSchemaAnnotation) 
     { 
      CallForEveryElementInternal((schemaObject as XmlSchemaAnnotation).Items, function); 
     } 
    } 

    private static void CallForEveryElementInternal(XmlSchemaObjectCollection schemaObjectCol, SchemaObjectFunction function) 
    { 
     foreach (XmlSchemaObject xso in schemaObjectCol) 
     { 
      CallForEveryElementInternal(xso, function); 
     } 
    } 

    #endregion 
+0

유용한 샘플이 있습니까? – paulwhit