public XmlElementPath(QualifiedName[] values)
 {
   elements = new QualifiedNameCollection(values);
 }
    public static XmlElementPath GetParentElementPathCore(string xml)
    {
      XmlElementPath path = new XmlElementPath();

      try
      {
        StringReader reader = new StringReader(xml);
        XmlTextReader xmlReader = new XmlTextReader(reader);
        xmlReader.XmlResolver = null;
        while (xmlReader.Read())
        {
          switch (xmlReader.NodeType)
          {
            case XmlNodeType.Element:
              if (!xmlReader.IsEmptyElement)
              {
                QualifiedName elementName = new QualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix);
                path.Elements.Add(elementName);
                path.Namespaces = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All);
              }
              break;
            case XmlNodeType.EndElement:
              path.Elements.RemoveLast();
              break;
          }
        }
      }
      catch (XmlException)
      {
        // Do nothing.
      }
      catch (WebException)
      {
        // Do nothing.
      }

      return path;
    }
    /// <summary>
    /// Gets the element name from the element start tag string.
    /// </summary>
    /// <param name="xml">This string must start at the
    /// element we are interested in.</param>
    static QualifiedName GetElementName(string xml)
    {
      string name = String.Empty;

      // Find the end of the element name.
      xml = xml.Replace("\r\n", " ");
      int index = xml.IndexOf(' ');
      if (index > 0)
      {
        name = xml.Substring(1, index - 1);
      }
      else
      {
        name = xml.Substring(1);
      }

      QualifiedName qualifiedName = new QualifiedName();

      int prefixIndex = name.IndexOf(':');
      if (prefixIndex > 0)
      {
        qualifiedName.Prefix = name.Substring(0, prefixIndex);
        qualifiedName.Name = name.Substring(prefixIndex + 1);
      }
      else
      {
        qualifiedName.Name = name;
      }

      return qualifiedName;
    }
 /// <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);
 }
    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>
    /// 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;
    }
    /// <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;
    }
    /// <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;
    }
    /// <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;
    }
    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;
    }