2009-12-30 29 views
3

我解析我的XSD文件。 (包括一些元素,complexTypes和simpleTypes)如何檢測XSD文件中的SimpleType?

我想通過類型屬性檢測它們中的每一個。

我的XSD文件有點像這樣。

<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. 
    } 
} 

我的示例代碼如何檢測屬性的類型是SimpleType的與它的財產或其他好的表現?

+0

你能改述這個問題?我不確定你想要做什麼。 – Diadistis 2009-12-30 10:01:24

+0

@Dadadistis,嗨,我重申了我的問題。 – 2009-12-30 10:09:50

+1

愚蠢的問題,但你的XML解析器沒有一個「elementName」方法,你可以用它來確定元素的名稱,從而解決該元素被命名爲「simpleType」或「complexType」。我不是指訪問屬性名稱,而是實際的元素名稱。 – 2010-01-07 17:09:28

回答

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文件的好方法。我認爲你給我另一個很好的解決方案。謝謝。 – 2010-01-08 10:25:40

1

您是否正在嘗試使用您定義的類型的模式的所有部分?該模式是否有合格的命名空間?如果是這樣,你應該能夠做到:

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

的XSD的命名空間將是模式的根元素上的targetNamespace 屬性。 (它稍微複雜一些,但不是很多)。

如果XSD沒有targetNamespace屬性或默認命名空間,那麼你可以指定任何你想要的,當你的架構添加到的XmlSchemaSet對象的添加方法和鑰匙關閉該。

如果這不是你想要做的,在C#和XSD中給出一個稍微更完整的例子。

+0

@tyranid,您是否正在嘗試使用由您定義的類型的模式的所有部分? - 答案是肯定的;和我的XSD文件TargetNamespace =「http://www.w3.org/2001/XMLSchema」,默認情況下已經定義。 – 2010-01-06 01:20:57

+0

@tyranid,我更新了我上面的原始帖子。謝謝。 – 2010-01-06 01:28:48