Exemple #1
0
        private IEnumerable <T> EvaluateIterator <T>(XPathNodeIterator result)
        {
            foreach (XPathNavigator navigator in result)
            {
                Debug.Assert(navigator.UnderlyingObject != null);
                object r = navigator.UnderlyingObject;
                if (!(r is T))
                {
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedEvaluation, r.GetType()));
                }
                yield return((T)r);

                XText?t = r as XText;
                if (t != null && t.GetParent() != null)
                {
                    do
                    {
                        t = t.NextNode as XText;
                        if (t == null)
                        {
                            break;
                        }
                        yield return((T)(object)t);
                    } while (t != t.GetParent() !.LastNode);
                }
            }
        }
Exemple #2
0
 public void Add(object?content)
 {
     AddContent(content);
     if (_text != null)
     {
         if (_parent.content == null)
         {
             if (_parent.SkipNotify())
             {
                 _parent.content = _text;
             }
             else
             {
                 if (_text.Length > 0)
                 {
                     InsertNode(new XText(_text));
                 }
                 else
                 {
                     if (_parent is XElement)
                     {
                         // Change in the serialization of an empty element:
                         // from empty tag to start/end tag pair
                         _parent.NotifyChanging(_parent, XObjectChangeEventArgs.Value);
                         if (_parent.content != null)
                         {
                             throw new InvalidOperationException(SR.InvalidOperation_ExternalCode);
                         }
                         _parent.content = _text;
                         _parent.NotifyChanged(_parent, XObjectChangeEventArgs.Value);
                     }
                     else
                     {
                         _parent.content = _text;
                     }
                 }
             }
         }
         else if (_text.Length > 0)
         {
             XText?prevXText = _previous as XText;
             if (prevXText != null && !(_previous is XCData))
             {
                 prevXText.Value += _text;
             }
             else
             {
                 _parent.ConvertTextToNode();
                 InsertNode(new XText(_text));
             }
         }
     }
 }
Exemple #3
0
        private static string CollectText(XText n)
        {
            string s = n.Value;

            if (n.GetParent() != null)
            {
                foreach (XNode node in n.NodesAfterSelf())
                {
                    XText?t = node as XText;
                    if (t == null)
                    {
                        break;
                    }
                    s += t.Value;
                }
            }
            return(s);
        }
Exemple #4
0
        /// <summary>
        /// Creates an <see cref="XPathNavigator"/> for a given <see cref="XNode"/>
        /// </summary>
        /// <param name="node">Extension point <see cref="XNode"/></param>
        /// <param name="nameTable">The <see cref="XmlNameTable"/> to be used by
        /// the <see cref="XPathNavigator"/></param>
        /// <returns>An <see cref="XPathNavigator"/></returns>
        public static XPathNavigator CreateNavigator(this XNode node, XmlNameTable?nameTable)
        {
            ArgumentNullException.ThrowIfNull(node);

            if (node is XDocumentType)
            {
                throw new ArgumentException(SR.Format(SR.Argument_CreateNavigator, XmlNodeType.DocumentType));
            }
            XText?text = node as XText;

            if (text != null)
            {
                if (text.GetParent() is XDocument)
                {
                    throw new ArgumentException(SR.Format(SR.Argument_CreateNavigator, XmlNodeType.Whitespace));
                }
                node = CalibrateText(text);
            }
            return(new XNodeNavigator(node, nameTable));
        }
        private void ValidateNodes(XElement e)
        {
            XNode?       n       = e.content as XNode;
            IXmlLineInfo orginal = SaveLineInfo(n);

            if (n != null)
            {
                do
                {
                    n = n.next !;
                    XElement?c = n as XElement;
                    if (c != null)
                    {
                        ValidateElement(c);
                    }
                    else
                    {
                        XText?t = n as XText;
                        if (t != null)
                        {
                            string s = t.Value;
                            if (s.Length > 0)
                            {
                                validator !.LineInfoProvider = t as IXmlLineInfo;
                                validator.ValidateText(s);
                            }
                        }
                    }
                } while (n != e.content);
                source = e;
            }
            else
            {
                string?s = e.content as string;
                if (s != null && s.Length > 0)
                {
                    validator !.ValidateText(s);
                }
            }
            RestoreLineInfo(orginal);
        }
Exemple #6
0
        private static XText CalibrateText(XText n)
        {
            XContainer?parentNode = n.GetParent();

            if (parentNode == null)
            {
                return(n);
            }
            foreach (XNode node in parentNode.Nodes())
            {
                XText?t          = node as XText;
                bool  isTextNode = t != null;
                if (isTextNode && node == n)
                {
                    return(t !);
                }
            }

            System.Diagnostics.Debug.Fail("Parent node doesn't contain itself.");
            return(null);
        }
Exemple #7
0
 private void AddNode(XNode n)
 {
     _parent.ValidateNode(n, _previous);
     if (n.parent != null)
     {
         n = n.CloneNode();
     }
     else
     {
         XNode p = _parent;
         while (p.parent != null)
         {
             p = p.parent;
         }
         if (n == p)
         {
             n = n.CloneNode();
         }
     }
     _parent.ConvertTextToNode();
     if (_text != null)
     {
         if (_text.Length > 0)
         {
             XText?prevXText = _previous as XText;
             if (prevXText != null && !(_previous is XCData))
             {
                 prevXText.Value += _text;
             }
             else
             {
                 InsertNode(new XText(_text));
             }
         }
         _text = null;
     }
     InsertNode(n);
 }