Exemplo n.º 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);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Compacts the path so it only contains the elements that are from
 /// the namespace of the last element in the path.
 /// </summary>
 /// <remarks>This method is used when we need to know the path for a
 /// particular namespace and do not care about the complete path.
 /// </remarks>
 public void Compact()
 {
     if (elements.HasItems)
     {
         XmlElementQualifiedName lastName = Elements.GetLast();
         int index = LastIndexNotMatchingNamespace(lastName.Namespace);
         if (index != -1)
         {
             elements.RemoveFirst(index + 1);
         }
     }
 }
Exemplo n.º 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);
        }
Exemplo n.º 4
0
 int LastIndexNotMatchingNamespace(string namespaceUri)
 {
     if (elements.Count > 1)
     {
         // Start the check from the last but one item.
         for (int i = elements.Count - 2; i >= 0; --i)
         {
             XmlElementQualifiedName name = elements[i];
             if (name.Namespace != namespaceUri)
             {
                 return(i);
             }
         }
     }
     return(-1);
 }
Exemplo n.º 5
0
        /// <summary>
        /// A qualified name is considered equal if the namespace and
        /// name are the same.  The prefix is ignored.
        /// </summary>
        public override bool Equals(object obj)
        {
            XmlElementQualifiedName xmlElementQualifiedName = obj as XmlElementQualifiedName;

            if (xmlElementQualifiedName != null)
            {
                return(this.xmlQualifiedName.Equals(xmlElementQualifiedName.xmlQualifiedName));
            }
            else
            {
                System.Xml.XmlQualifiedName name = obj as System.Xml.XmlQualifiedName;
                if (name != null)
                {
                    return(this.xmlQualifiedName.Equals(name));
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Source: Sharpdevelop
        /// 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 XmlElementQualifiedName 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);
            }

            return(XmlElementQualifiedName.FromString(name));
        }
Exemplo n.º 7
0
 public void AddElement(XmlElementQualifiedName elementName)
 {
     elements.Add(elementName);
 }