void GetChildElementCompletionData(XmlSchemaCompletionBuilder data, XmlSchemaObjectCollection items, string prefix)
 {
     foreach (XmlSchemaObject schemaObject in items)
     {
         var childElement = schemaObject as XmlSchemaElement;
         if (childElement != null)
         {
             string name = childElement.Name;
             if (name == null)
             {
                 name = childElement.RefName.Name;
                 var element = FindElement(childElement.RefName);
                 if (element != null)
                 {
                     if (element.IsAbstract)
                     {
                         AddSubstitionGroupElements(data, element.QualifiedName, prefix);
                     }
                     else
                     {
                         data.AddElement(name, prefix, element.Annotation);
                     }
                 }
                 else
                 {
                     data.AddElement(name, prefix, childElement.Annotation);
                 }
             }
             else
             {
                 data.AddElement(name, prefix, childElement.Annotation);
             }
             continue;
         }
         var childSequence = schemaObject as XmlSchemaSequence;
         if (childSequence != null)
         {
             GetChildElementCompletionData(data, childSequence.Items, prefix);
             continue;
         }
         var childChoice = schemaObject as XmlSchemaChoice;
         if (childChoice != null)
         {
             GetChildElementCompletionData(data, childChoice.Items, prefix);
             continue;
         }
         var groupRef = schemaObject as XmlSchemaGroupRef;
         if (groupRef != null)
         {
             GetChildElementCompletionData(data, groupRef, prefix);
             continue;
         }
     }
 }
 /// <summary>
 /// Adds any elements that have the specified substitution group.
 /// </summary>
 void AddSubstitionGroupElements(XmlSchemaCompletionBuilder data, XmlQualifiedName group, string prefix)
 {
     foreach (XmlSchemaElement element in schema.Elements.Values)
     {
         if (element.SubstitutionGroup == group)
         {
             data.AddElement(element.Name, prefix, element.Annotation);
         }
     }
 }
        /// <summary>
        /// Gets the possible root elements for an xml document using this schema.
        /// </summary>
        public async Task <CompletionContext> GetElementCompletionDataAsync(IAsyncCompletionSource source, string namespacePrefix, CancellationToken token)
        {
            await EnsureLoadedAsync();

            var builder = new XmlSchemaCompletionBuilder(source);

            foreach (XmlSchemaElement element in schema.Elements.Values)
            {
                if (element.Name != null)
                {
                    builder.AddElement(element.Name, namespacePrefix, element.Annotation);
                }
                else
                {
                    // Do not add reference element.
                }
            }
            return(new CompletionContext(builder.GetItems()));
        }