/// <summary> /// Source: Sharpdevelop /// Gets the active element path given the element text. /// </summary> static XmlElementInformation GetActiveElementStartPath(string xml, int index, string elementText) { XmlElementQualifiedName elementName = GetElementName(elementText); if (elementName.IsEmpty) { return(new XmlElementInformation()); } XmlNamespace elementNamespace = GetElementNamespace(elementText); XmlElementInformation path = GetFullParentElementPath(xml.Substring(0, index)); // Try to get a namespace for the active element's prefix. if (elementName.HasPrefix && !elementNamespace.HasName) { elementName.Namespace = path.GetNamespaceForPrefix(elementName.Prefix); elementNamespace.Name = elementName.Namespace; elementNamespace.Prefix = elementName.Prefix; } if (!elementNamespace.HasName) { if (path.Elements.Count > 0) { XmlElementQualifiedName parentName = path.Elements[path.Elements.Count - 1]; elementNamespace.Name = parentName.Namespace; elementNamespace.Prefix = parentName.Prefix; } } path.AddElement(new XmlElementQualifiedName(elementName.Name, elementNamespace)); return(path); }
/// <summary> /// Source: SharpDevelop /// </summary> /// <param name="xml"></param> /// <returns></returns> public static XmlElementInformation GetParentElementPath(string xml) { XmlElementInformation path = new XmlElementInformation(); IDictionary <string, string> namespacesInScope = null; using (StringReader reader = new StringReader(xml)) { using (XmlTextReader xmlReader = new XmlTextReader(reader)) { try { xmlReader.XmlResolver = null; // prevent XmlTextReader from loading external DTDs while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: if (!xmlReader.IsEmptyElement) { XmlElementQualifiedName elementName = new XmlElementQualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix); path.AddElement(elementName); } break; case XmlNodeType.EndElement: path.Elements.RemoveLast(); break; } } } catch (XmlException) { namespacesInScope = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All); } } } // Add namespaces in scope for the last element read. if (namespacesInScope != null) { foreach (KeyValuePair <string, string> ns in namespacesInScope) { path.NamespacesInScope.Add(new XmlNamespace(ns.Key, ns.Value)); } } return(path); }
/// <summary> /// Source: SharpDevelop /// </summary> /// <param name="xml"></param> /// <returns></returns> public static XmlElementInformation GetParentElementPath(string xml) { XmlElementInformation path = new XmlElementInformation(); IDictionary<string, string> namespacesInScope = null; using (StringReader reader = new StringReader(xml)) { using (XmlTextReader xmlReader = new XmlTextReader(reader)) { try { xmlReader.XmlResolver = null; // prevent XmlTextReader from loading external DTDs while (xmlReader.Read()) { switch (xmlReader.NodeType) { case XmlNodeType.Element: if (!xmlReader.IsEmptyElement) { XmlElementQualifiedName elementName = new XmlElementQualifiedName(xmlReader.LocalName, xmlReader.NamespaceURI, xmlReader.Prefix); path.AddElement(elementName); } break; case XmlNodeType.EndElement: path.Elements.RemoveLast(); break; } } } catch (XmlException) { namespacesInScope = xmlReader.GetNamespacesInScope(XmlNamespaceScope.All); } } } // Add namespaces in scope for the last element read. if (namespacesInScope != null) { foreach (KeyValuePair<string, string> ns in namespacesInScope) { path.NamespacesInScope.Add(new XmlNamespace(ns.Key, ns.Value)); } } return path; }