⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schemaparser.cs

📁 In the previous article, we presented an approach for capturing similarity between words that was co
💻 CS
📖 第 1 页 / 共 3 页
字号:
                {
                    return _schemas[i];
                }
            return null;
        }

        int count = 0;
        void Add_Element(TreeNode complexNode, string ns, XmlSchemaElement elem)
        {
            //add to complexNode
            XmlQualifiedName nodeName = XmlQualifiedName.Empty;

            if (!elem.RefName.IsEmpty)
            {
                XmlSchemaElement refElem = GetRefElement(elem);
                if (refElem == null) throw new Exception("Global element not found: " + elem.RefName);
                nodeName = elem.RefName;
                elem = refElem;

            }
            else
            {
                nodeName = elem.QualifiedName;
            }

            if (IsInfiniteRecursiveLoop(complexNode, nodeName)) return;
            //			++count;
            //			if (count > 300) return;

            TreeNode elemNode = new TreeNode(nodeName.Name);
            elemNode.ImageIndex = 1;
            elemNode.SelectedImageIndex = 1;

            if (nodeName.Namespace != string.Empty)
            {
                elemNode.Text = SchemaHelper.LoopupPrefix(_namespaces, nodeName.Namespace) + ":" + nodeName.Name;
            }

            elemNode.Tag = nodeName;
            complexNode.Nodes.Add(elemNode);
            string desc = GetAnnotation(elem.Annotation);


            NodeData data = new NodeData(elem, nodeName, string.Empty, desc, elem.DefaultValue);
            elemNode.Tag = data;

            if (!elem.SchemaTypeName.IsEmpty)
            {
                XmlSchemaComplexType ct = GetComplexTypeByName(elem.SchemaTypeName);


                if (ct != null)
                {
                    data.Type = ct.QualifiedName.Name;

                    if (IsInfiniteRecursiveLoop(complexNode, ct.QualifiedName)) return;

                    Parse_ComplexType(elemNode, ct, nodeName);
                }
                else
                    if (!elem.SchemaTypeName.IsEmpty)
                    {
                        data.Type = GetBuiltInTypeName(elem.SchemaTypeName);
                        string test = "";
                        if (BindingUse == SoapBindingUse.Encoded)
                            test = "type" + GetQualifiedNameString(complexNode, elem.SchemaTypeName)
                                + ((GetBuiltInTypeName(elem.SchemaTypeName)));
                    }
            }
            else if (elem.SchemaType == null)
            {
                data.Type = "any";
            }
            else
            {
                if (elem.SchemaType is XmlSchemaComplexType)
                    Parse_ComplexType(elemNode, (XmlSchemaComplexType)elem.SchemaType, nodeName);
                else
                    if (elem.SchemaType is XmlSchemaSimpleType)
                    {
                        XmlSchemaSimpleType st = elem.SchemaType as XmlSchemaSimpleType;
                        data.Type = GetBuiltInTypeName(st.QualifiedName);
                    }
            }


        }

        void Create_CustomType(TreeNode inner, XmlQualifiedName qname)
        {
            XmlSchemaComplexType ctype = GetComplexTypeByName(qname);
            TreeNode node = new TreeNode();
            inner.Nodes.Add(node);
            node.Text = qname.Name;

            if (ctype != null)
            {
                node.ImageIndex = 3;
                node.SelectedImageIndex = 3;

                Parse_ComplexType(node, ctype, qname);
                return;
            }

            XmlSchemaSimpleType stype = (XmlSchemaSimpleType)_schemas.Find(qname, typeof(XmlSchemaSimpleType));
            if (stype != null)
            {
                node.ImageIndex = 2;
                node.SelectedImageIndex = 2;

                Parse_SimpleType(node, stype);
                return;
            }

            inner.Tag = ((GetBuiltInTypeName(qname)));

            //throw new Exception ("Type not found: " + qname);
        }

        void Parse_ComplexType(TreeNode inner, XmlSchemaComplexType stype, XmlQualifiedName rootName)
        {
            Parse_ComplexType(inner, stype, rootName, -1);
        }

        void Parse_ComplexType(TreeNode innerNode, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
        {
            string ns = rootName.Namespace;


            if (rootName.Name.IndexOf("[]") != -1) rootName = arrayType;

            if (BindingUse == SoapBindingUse.Encoded)
            {
                innerNode.Text = SchemaHelper.LoopupPrefix(_namespaces, rootName.Namespace) + ":" + rootName.Name;
                //innerNode.Text = rootName.Namespace + ":" + rootName.Name;
                ns = string.Empty;
            }
            else
            {
                if (rootName.Namespace != string.Empty)
                    innerNode.Text = SchemaHelper.LoopupPrefix(_namespaces, rootName.Namespace) + ":" + rootName.Name;
                else
                    innerNode.Text = rootName.Name;
            }

            if (innerNode.Tag == null)
            {
                string desc = GetAnnotation(stype.Annotation);
                NodeData data = new NodeData(stype, rootName, "", desc, string.Empty);
                innerNode.Tag = data;
            }

            if (id != -1)
            {

                TreeNode node = new TreeNode("id:" + id);
                innerNode.Nodes.Add(node);

                if (rootName != arrayType)
                {
                    TreeNode node2 = new TreeNode("Type" + GetQualifiedNameString(innerNode, rootName));
                    node.Nodes.Add(node2);
                }
            }

            Add_ComplexAttributes(innerNode, stype);
            Add_ComplexElements(innerNode, ns, stype);
        }

        void Add_ComplexAttributes(TreeNode inner, XmlSchemaComplexType stype)
        {
            Add_Attributes(inner, stype.Attributes, stype.AnyAttribute);
        }

        void Add_ComplexElements(TreeNode complexNode, string ns, XmlSchemaComplexType stype)
        {

            if (stype.Particle != null)
            {
                Parse_ParticleComplexContent(complexNode, ns, stype.Particle);
            }
            else
            {
                if (stype.ContentModel is XmlSchemaSimpleContent)
                    Parse_SimpleContent(complexNode, (XmlSchemaSimpleContent)stype.ContentModel);
                else if (stype.ContentModel is XmlSchemaComplexContent)
                    Parse_ComplexContent(complexNode, ns, (XmlSchemaComplexContent)stype.ContentModel);
            }
        }

        void Add_Attributes(TreeNode innerNode, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat)
        {
            foreach (XmlSchemaObject at in atts)
            {
                if (at is XmlSchemaAttribute)
                {
                    XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
                    XmlSchemaAttribute refAttr = attr;

                    if (!attr.RefName.IsEmpty)
                    {
                        refAttr = GetRefAttribute(attr.RefName);
                        if (refAttr == null) throw new Exception("Global attribute not found: " + attr.RefName);
                    }

                    string type;
                    if (!refAttr.SchemaTypeName.IsEmpty) type = GetBuiltInTypeName(refAttr.SchemaTypeName);
                    else type = GetBuiltInType(refAttr.SchemaType);


                    TreeNode node = new TreeNode(refAttr.Name);
                    node.ImageIndex = 4;
                    node.SelectedImageIndex = 4;
                    string desc = GetAnnotation(refAttr.Annotation);
                    if (refAttr.QualifiedName.Namespace != string.Empty)
                    {
                        node.Text = SchemaHelper.LoopupPrefix(_namespaces, refAttr.QualifiedName.Namespace) + ":" + refAttr.Name;
                    }

                    NodeData data = new NodeData(at, refAttr.QualifiedName, type, desc, refAttr.DefaultValue);
                    node.Tag = data;

                    innerNode.Nodes.Add(node);

                }
                else if (at is XmlSchemaAttributeGroupRef)
                {
                    XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
                    XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup)_schemas.Find(gref.RefName, typeof(XmlSchemaAttributeGroup));
                    Add_Attributes(innerNode, grp.Attributes, grp.AnyAttribute);
                }
            }

            if (anyat != null)
            {
                TreeNode node = new TreeNode("any custom-attribute");
                innerNode.Nodes.Add(node);
            }
        }

        void Parse_ParticleComplexContent(TreeNode complexNode, string ns, XmlSchemaParticle particle)
        {
            Parse_ParticleContent(complexNode, ns, particle, false);
        }

        void Parse_ParticleContent(TreeNode complexNode, string ns, XmlSchemaParticle particle, bool multiValue)
        {
            if (particle is XmlSchemaGroupRef)
                particle = GetRefGroupParticle((XmlSchemaGroupRef)particle);

            if (particle.MaxOccurs > 1) multiValue = true;

            if (particle is XmlSchemaSequence)
            {
                Parse_SequenceContent(complexNode, ns, ((XmlSchemaSequence)particle).Items, multiValue);
            }
            else if (particle is XmlSchemaChoice)
            {
                if (((XmlSchemaChoice)particle).Items.Count == 1)
                    Parse_SequenceContent(complexNode, ns, ((XmlSchemaChoice)particle).Items, multiValue);
                else
                    Parse_ChoiceContent(complexNode, ns, (XmlSchemaChoice)particle, multiValue);
            }
            else if (particle is XmlSchemaAll)
            {
                Parse_SequenceContent(complexNode, ns, ((XmlSchemaAll)particle).Items, multiValue);
            }
        }

        void Parse_SequenceContent(TreeNode complexNode, string ns, XmlSchemaObjectCollection items, bool multiValue)
        {
            foreach (XmlSchemaObject item in items)
                Add_Item(complexNode, ns, item, multiValue);
        }

        void Add_Item(TreeNode complexNode, string ns, XmlSchemaObject item, bool multiValue)
        {
            if (item is XmlSchemaGroupRef)
                item = GetRefGroupParticle((XmlSchemaGroupRef)item);

            if (item is XmlSchemaElement)
            {
                XmlSchemaElement elem = (XmlSchemaElement)item;
                XmlSchemaElement refElem;
                if (!elem.RefName.IsEmpty) refElem = GetRefElement(elem);
                else refElem = elem;

                int num = (elem.MaxOccurs == 1 && !multiValue) ? 1 : 2;

                for (int n = 0; n < num; n++)
                {
                    if (BindingUse == SoapBindingUse.Literal)
                        Add_Element(complexNode, ns, refElem);
                    else
                        Add_RefType(complexNode, ns, refElem);
                }
            }
            else if (item is XmlSchemaAny)
            {
                TreeNode node = new TreeNode(("xml"));
                complexNode.Nodes.Add(node);
            }
            else if (item is XmlSchemaParticle)
            {
                Parse_ParticleContent(complexNode, ns, (XmlSchemaParticle)item, multiValue);
            }
        }

        void Parse_ChoiceContent(TreeNode inner, string ns, XmlSchemaChoice choice, bool multiValue)
        {
            foreach (XmlSchemaObject item in choice.Items)
                Add_Item(inner, ns, item, multiValue);
        }

        void Parse_SimpleContent(TreeNode inner, XmlSchemaSimpleContent content)
        {
            XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
            XmlSchemaSimpleContentRestriction rst = content.Content as XmlSchemaSimpleContentRestriction;

            if (ext != null)
                Add_Attributes(inner, ext.Attributes, ext.AnyAttribute);
            else
                if (rst != null)
                    Add_Attributes(inner, rst.Attributes, rst.AnyAttribute);

            XmlQualifiedName qname = GetContentBaseType(content.Content);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -