Пример #1
0
        /// <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);
        }
Пример #2
0
        /// <summary>
        /// An xml element path is considered to be equal if
        /// each path item has the same name and namespace.
        /// </summary>
        public override bool Equals(object obj)
        {
            XmlElementInformation rhsPath = obj as XmlElementInformation;

            if (rhsPath == null)
            {
                return(false);
            }

            return(elements.Equals(rhsPath.elements));
        }
Пример #3
0
        /// <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);
        }