2009-12-30 7 views
3

XSD 파일을 구문 분석하고 있습니다. (일부 요소, complexTypes 및 simpleTypes 포함)XSD 파일에서 SimpleType을 검색하는 방법은 무엇입니까?

각 속성을 유형별로 검색하려고합니다.

내 XSD 파일이 이와 비슷합니다. 는 IT의 재산 또는 다른 좋은 표현과 함께하는 SimpleType

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="mainInfo"> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element ref="DocumentInfo" minOccurs="1" maxOccurs="1" /> 
     <xsd:element ref="Prerequisite" minOccurs="1" maxOccurs="1" /> 
     </xsd:sequence> 
    </xsd:complexType> 
    </xsd:element> 
    <!-- Element of DocumentInfo --> 
    <xsd:element name="DocumentInfo"> 
    <xsd:complexType> 
     <xsd:attribute name="Name" type="xsd:string" /> 
     <xsd:attribute name="Description" type="xsd:string" /> 
    </xsd:complexType> 
    </xsd:element> 
    <!-- Element of Prerequisite --> 
    <xsd:element name="Prerequisite"> 
    <xsd:complexType> 
     <xsd:sequence> 
     <xsd:element name="Type" type="Prerequisite.Type.type" minOccurs="1" maxOccurs="1" /> 
     </xsd:sequence> 
    </xsd:complexType> 
    </xsd:element> 
    <xsd:complexType name="Prerequisite.Type.type"> 
    <xsd:attribute name="SystemType" type="SystemTypeEnum" /> 
    </xsd:complexType> 
    <xsd:simpleType name="SystemTypeEnum"> 
    <xsd:restriction base="xsd:string"> 
     <xsd:enumeration value="Linux" /> 
    </xsd:restriction> 
    </xsd:simpleType> 
</xsd:schema> 

// Add the customer schema to a new XmlSchemaSet and compile it. 
     // Any schema validation warnings and errors encountered reading or 
     // compiling the schema are handled by the ValidationEventHandler delegate. 
     XmlSchemaSet schemaSet = new XmlSchemaSet(); 
     schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); 
     schemaSet.Add("http://www.w3.org/2001/XMLSchema", "D:\\TMP\\test.xsd"); 
     schemaSet.Compile(); 

     // Retrieve the compiled XmlSchema object from the XmlSchemaSet 
     // by iterating over the Schemas property. 
     XmlSchema customerSchema = null; 
     foreach (XmlSchema schema in schemaSet.Schemas()) 
     { 
      customerSchema = schema; 
     } 



foreach (XmlSchemaElement element in customerSchema.Elements.Values) 
{ 
    XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType; 
    // currently, i detect my ComplexType via the method below. 
    if (aSchemaType.TypeCode == XmlTypeCode.None) 
    { 
    // Insert some code to handle ComplexType obj 
     // Handle Elements of XSD File 
     foreach (XmlSchemaElement element in customerSchema.Elements.Values) 
     { 
      XmlSchemaComplexType complexType = element.ElementSchemaType as XmlSchemaComplexType; 
      Dictionary<string, object> aTempDict = new Dictionary<string,object>(); 

      mainDict.Add(element.Name, aTempDict); 
      //Parse elements 
      parseElement(complexType, ref aTempDict); 
      break; 
     } 
    } 

    // i want to find a method to detect my SimpleTYpe 
    // a little like this way, but i don't want to compare the equal or not with some string value. (NO == "string", thanks.) 
    else if (attribute.AttributeSchemaType.TypeCode == ???) 
    // else if (Some other method to detect SimpleType in a XSD file) 
    { 
     // Insert some code to handle SimpleType obj 
     // Loop the XSD Node and find out all the SimpleTye objects(members and type values), then add them to the related sub Dictionary based on ComplexType elements **TYPE** defined. 
    } 
} 

아래 내 샘플 코드는 어떻게 속성의 유형을 감지 할 수있다?

+0

당신을 수 질문을 바꾸세요? 네가하려는 일을 잘 모르겠다. – Diadistis

+0

@Diadistis, 안녕하세요, 저는 제 질문에 대해 다시 말했습니다. –

+1

멍청한 질문이지만 XML 파서에는 요소의 이름을 결정하는 데 사용할 수있는 "elementName"메서드가 없으므로 요소의 이름이 "simpleType"또는 "complexType"인 것으로 확인됩니다. 나는 속성 이름에 대한 액세스가 아니라 실제 요소 이름에 대한 액세스를 의미합니다. –

답변

3

스키마를 구문 분석하려는 경우 자습서 How Do I...Use the Xml Schema Object Model?에서 this code sample을 살펴보아야합니다. (필자는 제한이 완전히 구현되지 않은 것을 알 수 않았다 -는 제한 기본 유형 또는 측면을하지 않습니다.)

을이 같은 줄 것이다 코드 샘플에 적용 :

// Add the customer schema to a new XmlSchemaSet and compile it. 
    // Any schema validation warnings and errors encountered reading or 
    // compiling the schema are handled by the ValidationEventHandler delegate. 
    XmlSchemaSet schemaSet = new XmlSchemaSet(); 
    schemaSet.ValidationEventHandler += new ValidationEventHandler(ValidationCallback); 
    schemaSet.Add("http://www.w3.org/2001/XMLSchema", "D:\\TMP\\test.xsd"); 
    schemaSet.Compile(); 

    // Retrieve the compiled XmlSchema object from the XmlSchemaSet 
    // by iterating over the Schemas property. 
    XmlSchema customerSchema = null; 
    foreach (XmlSchema schema in schemaSet.Schemas()) 
    { 
     customerSchema = schema; 
    } 

    // Iterate over all schema items 
    foreach (object item in xmlSchema.Items) 
    { 
     if (item is XmlSchemaAttribute) 
     { 
     } 
     else if (item is XmlSchemaComplexType) 
     { 
     } 
     else if (item is XmlSchemaSimpleType) 
     { 
      XmlSchemaSimpleType simpleType = item as XmlSchemaSimpleType; 
      Console.WriteLine("SimpleType found with name=" + simpleType.Name); 
     } 
     else if (item is XmlSchemaElement) 
     { 
     } 
     else if (item is XmlSchemaAnnotation) 
     { 
     } 
     else if (item is XmlSchemaAttributeGroup) 
     { 
     } 
     else if (item is XmlSchemaNotation) 
     { 
     } 
     else if (item is XmlSchemaGroup) 
     { 
     } 
     else 
     { 
      Console.WriteLine("Not Implemented."); 
     } 
    } 
+0

안녕 Tuzo. foreach (xmlSchema.Items의 오브젝트 항목)을 사용하여 XSD 파일을 판단하는 좋은 방법입니다. 나는 네가 나를 또 다른 좋은 해결책으로 만들었다 고 생각한다. 감사. –

1

정의한 유형을 사용하는 스키마의 모든 부분을 해결하려고합니까? 스키마에 적합한 네임 스페이스가 있습니까? 그래서 당신은 할 수 있어야하는 경우 :

 
XmlSchemaType type = element.ElementSchemaType; 
// Or type = attribute.AttributeSchemaType; 
if(type.QualifiedName.Namespace == "http://mynamespace.com") { 
    // Your type 
} 

는 XSD의 네임 스페이스는 스키마의 루트 요소에 의 targetNamespace 속성에있을 것입니다. (다소 복잡하지만 그다지 복잡하지는 않습니다.)

XSD를 다음 당신은 당신이 떨어져 XmlSchemaSet에 객체의 추가 방법과 키를 스키마를 추가 할 때 원하는대로 지정할 수있는에 targetNamespace 속성 또는 기본 네임 스페이스가없는 경우

.

C# 및 XSD에서 약간 더 완전한 예제를 제공하십시오.

+0

@tyranid 당신이 정의한 유형을 사용하는 스키마의 모든 부분을 해결하려고합니까? - 대답은 YES입니다. 및 XSD 파일 TargetNamespace = "http://www.w3.org/2001/XMLSchema"가 이미 기본적으로 정의되어 있습니다. –

+0

@tyranid, 위의 원래 게시물을 업데이트했습니다. 고맙습니다. –

관련 문제