如何在 XSD 中获取 simpleType 的 maxlength/pattern 值 [英] How to get maxlength/pattern value of a simpleType in an XSD

查看:55
本文介绍了如何在 XSD 中获取 simpleType 的 maxlength/pattern 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历一个 xsd 以获取每个属性的类型,然后获取该类型的值,但我只能检索类型.

I want to traverse an xsd in order to get each attribute's type and after that that type's value but I am stuck in just retrieving the type.

到目前为止,我已经通过递归成功地遍历了 xsd 树

So far I have successfully traversed the xsd tree via recursion

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):

And here is the simpleType's value (maxLength) i want to retrieve:

 <xs:simpleType name="String100">
    <xs:restriction base="xs:string">
      <xs:maxLength value="100" />
    </xs:restriction>
  </xs:simpleType>

我需要获取值 100 并将其保存在一个变量中以备后用.

I need to get the value 100 and save it in a variable for later use.

在我提供的这段代码中,查看第一个 foreach 循环.一旦我们到达一个没有其他子元素作为 complexType 的 complexType,我们就会到达那里.所以我们已经到达了最后一个 complexType 及其唯一的孩子 xs:attributes.现在,当我投射属性时,我可以检索名称和类型.但我需要的是从代码段最后提供的约束中收集类型的值.

In the piece of code I provided look at the first foreach loop. We get there once we get to an complexType which has no other children as complexTypes. So we have reached the last complexType with its only children xs:attributes. Now, as I cast the attributes I can retrieve the name and type. But what I need is to gather the type's value from the constraint provided last in the code piece.

P.S 我知道我可以使用类型的名称string100"并将其子串化以获得 100 并将其解析为 int,但如果不是这种情况怎么办?

P.S I know that i could take the type's name " string100" and substring it to get the 100 and parse it to int, but what if this is not the case?

推荐答案

这一直可用,看起来很简单:

This has been available the whole time and it look simple:

using (XmlReader schemaReader = XmlReader.Create(new StringReader(the_schema_as_string)))
        {
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.Add(XmlSchema.Read(schemaReader, null));

            var xsdAsXDoc = XDocument.Load(new StringReader(the_schema_as_string));
            var ns = XNamespace.Get(@"http://www.w3.org/2001/XMLSchema");

            foreach (XmlSchemaElement element in schemaSet.Schemas()
                .Cast<XmlSchema>()
                .SelectMany(s => s.Elements.Values.Cast<XmlSchemaElement>()))
            {
                GetFieldMaxLengthValue(element, xsdAsXDoc, ns);
            }
        }

您从 foreach 检索的元素有一个属性Parent",当您从那里单击父元素时,您可以找到整个类型.在我被直接卡在根元素之前,我一直应该去父元素并从那里搜索简单类型!

The element you retrieve from the foreach has a property "Parent" and as you click the parent from there you can find the whole types. Before I was directly stuck to the root element and the whole time I was supposed to go to the parent and from there to search for the simple types!

这篇关于如何在 XSD 中获取 simpleType 的 maxlength/pattern 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆