我正在解析我的XSD文件。 (包括一些元素,complexTypes和simpleTypes)
我想通过type属性检测每一个。
我的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 :(得分:3)
如果您只是想解析架构,那么您应该从教程this code sample中查看How Do I...Use the Xml Schema Object Model?。 (我确实注意到限制没有完全实现 - 它没有得到限制基类型或任何方面。)
应用于您的代码示例将提供如下内容:
// 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.");
}
}
答案 1 :(得分:1)
您是否尝试计算使用您定义的类型的架构的所有部分?架构是否具有合格的命名空间?如果是这样,你应该能够做到:
XmlSchemaType type = element.ElementSchemaType; // Or type = attribute.AttributeSchemaType; if(type.QualifiedName.Namespace == "http://mynamespace.com") { // Your type }
XSD的命名空间将位于架构根元素的 targetNamespace 属性中。 (它稍微复杂一些,但并不多)。
如果XSD没有targetNamespace属性或默认命名空间,那么您可以在将模式添加到 XmlSchemaSet 对象的添加方法时指定您想要的任何内容并关闭它。< / p>
如果这不是你要做的,那么在C#和XSD中给出一个稍微全面的例子。