我想遍历一个 xsd 以获取每个属性的类型,然后获取该类型的值,但我只停留在检索类型上。
至此我已经通过递归成功遍历了xsd树
private int GetFieldMaxLengthValue(XmlSchemaObject element)
{
if(element is XmlSchemaComplexType)
{
var complextType = element as XmlSchemaComplexType;
if(complextType.Particle == null)
{
var attributes = complextType.Attributes;
foreach (var attribute in attributes)
{
var schemaAttribute = attribute as XmlSchemaAttribute;
var attributeSchemaType = schemaAttribute.SchemaTypeName.Name;
}
}
else
{
GetFieldMaxLengthValue(complextType.Particle);
}
}
else if(element is XmlSchemaSequence)
{
var sequence = element as XmlSchemaSequence;
foreach (XmlSchemaObject item in sequence.Items)
{
var schemaType = item as XmlSchemaElement;
GetFieldMaxLengthValue(schemaType.SchemaType);
}
}
return 0;
}
这是我要检索的 simpleType 的值 (maxLength):
<xs:simpleType name="String100">
<xs:restriction base="xs:string">
<xs:maxLength value="100" />
</xs:restriction>
</xs:simpleType>
我需要获取值 100 并将其保存在一个变量中供以后使用。
在我提供的代码片段中,查看第一个 foreach 循环。一旦我们到达一个没有其他孩子作为 complexTypes 的 complexType,我们就到了那里。所以我们已经到达了最后一个 complexType 及其唯一的孩子 xs:attributes。现在,当我转换属性时,我可以检索名称和类型。但我需要的是从代码段最后提供的约束中收集类型的值。
PS我知道我可以采用类型的名称“string100”并将其子字符串化以获得 100 并将其解析为 int,但如果不是这种情况怎么办?
不负相思意
侃侃尔雅
相关分类