/// <summary> /// Gets the parent element path based on the index position. /// </summary> public static XmlElementPath GetParentElementPath(string xml) { XmlElementPath path = new XmlElementPath(); try { using (var reader = new StringReader(xml)) { using (var 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); } break; case XmlNodeType.EndElement: path.Elements.RemoveLast(); break; } } } } } catch (XmlException) { //MessageBox.Show(e.Message); } catch (WebException) { // Do nothing. } path.Compact(); return path; }
/// <summary> /// Gets the child element completion data for the xml element that exists /// at the end of the specified path. /// </summary> public ICompletionData[] GetChildElementCompletionData(XmlElementPath path) { XmlCompletionDataCollection data = new XmlCompletionDataCollection(); // Locate matching element. XmlSchemaElement element = FindElement(path); // Get completion data. if (element != null) { data = GetChildElementCompletionData(element, path.Elements.LastPrefix); } return data.ToArray(); }
/// <summary> /// Gets path of the xml element start tag that the specified /// <paramref name="index"/> is currently inside. /// </summary> /// <remarks>If the index outside the start tag then an empty path /// is returned.</remarks> public static XmlElementPath GetActiveElementStartPath(string xml, int index) { XmlElementPath path = new XmlElementPath(); string elementText = GetActiveElementStartText(xml, index); if (elementText != null) { QualifiedName elementName = GetElementName(elementText); NamespaceURI elementNamespace = GetElementNamespace(elementText); path = GetParentElementPath(xml.Substring(0, index)); if (elementNamespace.Namespace.Length == 0) { if (path.Elements.Count > 0) { QualifiedName parentName = path.Elements[path.Elements.Count - 1]; elementNamespace.Namespace = parentName.Namespace; elementNamespace.Prefix = parentName.Prefix; } } path.Elements.Add(new QualifiedName(elementName.Name, elementNamespace.Namespace, elementNamespace.Prefix)); path.Compact(); } return path; }
/// <summary> /// Gets the autocomplete data for the specified attribute value. /// </summary> public ICompletionData[] GetAttributeValueCompletionData(XmlElementPath path, string name) { XmlCompletionDataCollection data = new XmlCompletionDataCollection(); // Locate matching element. XmlSchemaElement element = FindElement(path); // Get completion data. if (element != null) { data = GetAttributeValueCompletionData(element, name); } return data.ToArray(); }
/// <summary> /// Gets the attribute completion data for the xml element that exists /// at the end of the specified path. /// </summary> public ICompletionData[] GetAttributeCompletionData(XmlElementPath path) { XmlCompletionDataCollection data = new XmlCompletionDataCollection(); // Locate matching element. XmlSchemaElement element = FindElement(path); // Get completion data. if (element != null) { prohibitedAttributes.Clear(); data = GetAttributeCompletionData(element); } return data.ToArray(); }
/// <summary> /// Finds the element that exists at the specified path. /// </summary> /// <remarks>This method is not used when generating completion data, /// but is a useful method when locating an element so we can jump /// to its schema definition.</remarks> /// <returns><see langword="null"/> if no element can be found.</returns> public XmlSchemaElement FindElement(XmlElementPath path) { XmlSchemaElement element = null; for (int i = 0; i < path.Elements.Count; ++i) { QualifiedName name = path.Elements[i]; if (i == 0) { // Look for root element. element = FindElement(name); if (element == null) { break; } } else { element = FindChildElement(element, name); if (element == null) { break; } } } return element; }
ICompletionData[] GetChildElementCompletionData(XmlElementPath path) { ICompletionData[] completionData = null; XmlSchemaCompletionData schema = defaultSchemaCompletionData; if (schema != null) { completionData = schema.GetChildElementCompletionData(path); } return completionData; }
ICompletionData[] GetAttributeValueCompletionData(XmlElementPath path, string name) { ICompletionData[] completionData = null; XmlSchemaCompletionData schema = defaultSchemaCompletionData; if (schema != null) { completionData = schema.GetAttributeValueCompletionData(path, name); } return completionData; }