Пример #1
0
        public static int GetIndex(this XmlNode thisValue, XmlIndexMatchType matchType)
        {
            if (thisValue?.ParentNode == null)
            {
                return(-1);
            }

            int index = 0;

            if (matchType == XmlIndexMatchType.None)
            {
                for (XmlNode n = thisValue; n != null; n = n.PreviousSibling)
                {
                    ++index;
                }

                return(index);
            }

            bool matchT = matchType.FastHasFlag(XmlIndexMatchType.Type);
            bool matchN = thisValue.NodeType == XmlNodeType.Element && matchType.FastHasFlag(XmlIndexMatchType.Name);

            if (matchN)
            {
                for (XmlNode n = thisValue; n != null; n = n.PreviousSibling)
                {
                    if (matchT && n.NodeType != thisValue.NodeType)
                    {
                        continue;
                    }
                    if (!n.Name.IsSame(thisValue.Name))
                    {
                        continue;
                    }
                    ++index;
                }
            }
            else
            {
                for (XmlNode n = thisValue; n != null; n = n.PreviousSibling)
                {
                    if (matchT && n.NodeType != thisValue.NodeType)
                    {
                        continue;
                    }
                    ++index;
                }
            }

            return(index);
        }
Пример #2
0
        public static int GetIndex(this XNode thisValue, XmlIndexMatchType matchType)
        {
            if (thisValue?.Parent == null)
            {
                return(0);
            }

            int index = 0;

            if (matchType == XmlIndexMatchType.None)
            {
                for (XNode n = thisValue; n != null; n = n.PreviousNode)
                {
                    ++index;
                }

                return(index);
            }

            bool   matchT   = matchType.FastHasFlag(XmlIndexMatchType.Type);
            string thisName = thisValue.NodeType == XmlNodeType.Element && matchType.FastHasFlag(XmlIndexMatchType.Name) ? ((XElement)thisValue).Name.LocalName : null;

            if (thisName != null)
            {
                for (XNode n = thisValue; n != null; n = n.PreviousNode)
                {
                    if (matchT && n.NodeType != thisValue.NodeType)
                    {
                        continue;
                    }
                    if (n is not XElement element || !element.Name.LocalName.IsSame(thisName))
                    {
                        continue;
                    }
                    ++index;
                }
            }
            else
            {
                for (XNode n = thisValue; n != null; n = n.PreviousNode)
                {
                    if (matchT && n.NodeType != thisValue.NodeType)
                    {
                        continue;
                    }
                    ++index;
                }
            }

            return(index);
        }
Пример #3
0
        public static int GetIndex(this XElement thisValue, bool matchName)
        {
            if (thisValue == null)
            {
                return(-1);
            }
            XmlIndexMatchType flags = XmlIndexMatchType.Type;

            if (matchName)
            {
                flags |= XmlIndexMatchType.Name;
            }
            return(thisValue.GetIndex(flags));
        }