示例#1
0
        /// <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);
        }
示例#2
0
        /// <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);
        }
示例#3
0
        /// <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;
        }
示例#4
0
        /// <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;
        }