Exemplo n.º 1
0
 public XmlElementPath(QualifiedName[] values)
 {
     elements = new QualifiedNameCollection(values);
 }
        /// <summary>
        /// Finds the element in the collection of schema objects.
        /// </summary>
        XmlSchemaElement FindElement(XmlSchemaObjectCollection items, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;

              foreach (XmlSchemaObject schemaObject in items)
              {
            XmlSchemaElement element = schemaObject as XmlSchemaElement;
            XmlSchemaSequence sequence = schemaObject as XmlSchemaSequence;
            XmlSchemaChoice choice = schemaObject as XmlSchemaChoice;
            XmlSchemaGroupRef groupRef = schemaObject as XmlSchemaGroupRef;

            if (element != null)
            {
              if (element.Name != null)
              {
            if (name.Name == element.Name)
            {
              matchedElement = element;
            }
              }
              else if (element.RefName != null)
              {
            if (name.Name == element.RefName.Name)
            {
              matchedElement = FindElement(element.RefName);
            }
            else
            {
              // Abstract element?
              XmlSchemaElement abstractElement = FindElement(element.RefName);
              if (abstractElement != null && abstractElement.IsAbstract)
              {
                matchedElement = FindSubstitutionGroupElement(abstractElement.QualifiedName, name);
              }
            }
              }
            }
            else if (sequence != null)
            {
              matchedElement = FindElement(sequence.Items, name);
            }
            else if (choice != null)
            {
              matchedElement = FindElement(choice.Items, name);
            }
            else if (groupRef != null)
            {
              matchedElement = FindElement(groupRef, name);
            }

            // Did we find a match?
            if (matchedElement != null)
            {
              break;
            }
              }

              return matchedElement;
        }
        XmlSchemaElement FindElement(XmlSchemaGroupRef groupRef, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;

              XmlSchemaGroup group = FindGroup(groupRef.RefName.Name);
              if (group != null)
              {
            XmlSchemaSequence sequence = group.Particle as XmlSchemaSequence;
            XmlSchemaChoice choice = group.Particle as XmlSchemaChoice;

            if (sequence != null)
            {
              matchedElement = FindElement(sequence.Items, name);
            }
            else if (choice != null)
            {
              matchedElement = FindElement(choice.Items, name);
            }
              }

              return matchedElement;
        }
        /// <summary>
        /// Finds the named child element contained in the extension element.
        /// </summary>
        XmlSchemaElement FindChildElement(XmlSchemaComplexContentExtension extension, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;

              XmlSchemaComplexType complexType = FindNamedType(schema, extension.BaseTypeName);
              if (complexType != null)
              {
            matchedElement = FindChildElement(complexType, name);

            if (matchedElement == null)
            {

              XmlSchemaSequence sequence = extension.Particle as XmlSchemaSequence;
              XmlSchemaChoice choice = extension.Particle as XmlSchemaChoice;
              XmlSchemaGroupRef groupRef = extension.Particle as XmlSchemaGroupRef;

              if (sequence != null)
              {
            matchedElement = FindElement(sequence.Items, name);
              }
              else if (choice != null)
              {
            matchedElement = FindElement(choice.Items, name);
              }
              else if (groupRef != null)
              {
            matchedElement = FindElement(groupRef, name);
              }
            }
              }

              return matchedElement;
        }
        /// <summary>
        /// Finds the named child element contained in the restriction element.
        /// </summary>
        XmlSchemaElement FindChildElement(XmlSchemaComplexContentRestriction restriction, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;
              XmlSchemaSequence sequence = restriction.Particle as XmlSchemaSequence;
              XmlSchemaGroupRef groupRef = restriction.Particle as XmlSchemaGroupRef;

              if (sequence != null)
              {
            matchedElement = FindElement(sequence.Items, name);
              }
              else if (groupRef != null)
              {
            matchedElement = FindElement(groupRef, name);
              }

              return matchedElement;
        }
        XmlSchemaElement FindChildElement(XmlSchemaComplexType complexType, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;

              XmlSchemaSequence sequence = complexType.Particle as XmlSchemaSequence;
              XmlSchemaChoice choice = complexType.Particle as XmlSchemaChoice;
              XmlSchemaGroupRef groupRef = complexType.Particle as XmlSchemaGroupRef;
              XmlSchemaAll all = complexType.Particle as XmlSchemaAll;
              XmlSchemaComplexContent complexContent = complexType.ContentModel as XmlSchemaComplexContent;

              if (sequence != null)
              {
            matchedElement = FindElement(sequence.Items, name);
              }
              else if (choice != null)
              {
            matchedElement = FindElement(choice.Items, name);
              }
              else if (complexContent != null)
              {
            XmlSchemaComplexContentExtension extension = complexContent.Content as XmlSchemaComplexContentExtension;
            XmlSchemaComplexContentRestriction restriction = complexContent.Content as XmlSchemaComplexContentRestriction;
            if (extension != null)
            {
              matchedElement = FindChildElement(extension, name);
            }
            else if (restriction != null)
            {
              matchedElement = FindChildElement(restriction, name);
            }
              }
              else if (groupRef != null)
              {
            matchedElement = FindElement(groupRef, name);
              }
              else if (all != null)
              {
            matchedElement = FindElement(all.Items, name);
              }

              return matchedElement;
        }
        /// <summary>
        /// Finds an element that matches the specified <paramref name="name"/>
        /// from the children of the given <paramref name="element"/>.
        /// </summary>
        XmlSchemaElement FindChildElement(XmlSchemaElement element, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;

              XmlSchemaComplexType complexType = GetElementAsComplexType(element);
              if (complexType != null)
              {
            matchedElement = FindChildElement(complexType, name);
              }

              return matchedElement;
        }
 /// <summary>
 /// Finds an element in the schema.
 /// </summary>
 /// <remarks>
 /// Only looks at the elements that are defined in the 
 /// root of the schema so it will not find any elements
 /// that are defined inside any complex types.
 /// </remarks>
 public XmlSchemaElement FindElement(QualifiedName name)
 {
     foreach (XmlSchemaElement element in schema.Elements.Values)
       {
     if (element.QualifiedName == null && name.Equals(new QualifiedName(element.Name, namespaceUri)))
     {
       return element;
     }
     else if (name.Equals(element.QualifiedName))
     {
       return element;
     }
       }
       return null;
 }
 /// <summary>
 /// Finds the complex type with the specified name.
 /// </summary>
 public XmlSchemaComplexType FindComplexType(QualifiedName name)
 {
     XmlQualifiedName qualifiedName = new XmlQualifiedName(name.Name, name.Namespace);
       return FindNamedType(schema, qualifiedName);
 }
        /// <summary>
        /// Looks for the substitution group element of the specified name.
        /// </summary>
        XmlSchemaElement FindSubstitutionGroupElement(XmlQualifiedName group, QualifiedName name)
        {
            XmlSchemaElement matchedElement = null;

              foreach (XmlSchemaElement element in schema.Elements.Values)
              {
            if (element.SubstitutionGroup == group)
            {
              if (element.Name != null)
              {
            if (element.Name == name.Name)
            {
              matchedElement = element;
              break;
            }
              }
            }
              }

              return matchedElement;
        }