Esempio n. 1
0
        internal void ReadContentFrom(XmlReader r)
        {
            if (r.ReadState != ReadState.Interactive)
            {
                throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
            }
            XContainer     c      = this;
            NamespaceCache eCache = new NamespaceCache();
            NamespaceCache aCache = new NamespaceCache();

            do
            {
                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                    XElement e = new XElement(eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            e.AppendAttributeSkipNotify(new XAttribute(aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value));
                        } while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    c.AddNodeSkipNotify(e);
                    if (!r.IsEmptyElement)
                    {
                        c = e;
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (c.content == null)
                    {
                        c.content = string.Empty;
                    }
                    if (c == this)
                    {
                        return;
                    }
                    c = c.parent;
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                    c.AddStringSkipNotify(r.Value);
                    break;

                case XmlNodeType.CDATA:
                    c.AddNodeSkipNotify(new XCData(r.Value));
                    break;

                case XmlNodeType.Comment:
                    c.AddNodeSkipNotify(new XComment(r.Value));
                    break;

                case XmlNodeType.ProcessingInstruction:
                    c.AddNodeSkipNotify(new XProcessingInstruction(r.Name, r.Value));
                    break;

                case XmlNodeType.DocumentType:
                    c.AddNodeSkipNotify(new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value));
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
                }
            } while (r.Read());
        }
Esempio n. 2
0
        internal void ReadContentFrom(XmlReader r, LoadOptions o)
        {
            if ((o & (LoadOptions.SetBaseUri | LoadOptions.SetLineInfo)) == 0)
            {
                ReadContentFrom(r);
                return;
            }
            if (r.ReadState != ReadState.Interactive)
            {
                throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive);
            }
            XContainer     c       = this;
            XNode          n       = null;
            NamespaceCache eCache  = new NamespaceCache();
            NamespaceCache aCache  = new NamespaceCache();
            string         baseUri = (o & LoadOptions.SetBaseUri) != 0 ? r.BaseURI : null;
            IXmlLineInfo   li      = (o & LoadOptions.SetLineInfo) != 0 ? r as IXmlLineInfo : null;

            do
            {
                string uri = r.BaseURI;
                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                {
                    XElement e = new XElement(eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (baseUri != null && baseUri != uri)
                    {
                        e.SetBaseUri(uri);
                    }
                    if (li != null && li.HasLineInfo())
                    {
                        e.SetLineInfo(li.LineNumber, li.LinePosition);
                    }
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            XAttribute a = new XAttribute(aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                            if (li != null && li.HasLineInfo())
                            {
                                a.SetLineInfo(li.LineNumber, li.LinePosition);
                            }
                            e.AppendAttributeSkipNotify(a);
                        } while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    c.AddNodeSkipNotify(e);
                    if (!r.IsEmptyElement)
                    {
                        c = e;
                        if (baseUri != null)
                        {
                            baseUri = uri;
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                {
                    if (c.content == null)
                    {
                        c.content = string.Empty;
                    }
                    // Store the line info of the end element tag.
                    // Note that since we've got EndElement the current container must be an XElement
                    XElement e = c as XElement;
                    Debug.Assert(e != null, "EndElement received but the current container is not an element.");
                    if (e != null && li != null && li.HasLineInfo())
                    {
                        e.SetEndElementLineInfo(li.LineNumber, li.LinePosition);
                    }
                    if (c == this)
                    {
                        return;
                    }
                    if (baseUri != null && c.HasBaseUri)
                    {
                        baseUri = c.parent.BaseUri;
                    }
                    c = c.parent;
                    break;
                }

                case XmlNodeType.Text:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                    if ((baseUri != null && baseUri != uri) ||
                        (li != null && li.HasLineInfo()))
                    {
                        n = new XText(r.Value);
                    }
                    else
                    {
                        c.AddStringSkipNotify(r.Value);
                    }
                    break;

                case XmlNodeType.CDATA:
                    n = new XCData(r.Value);
                    break;

                case XmlNodeType.Comment:
                    n = new XComment(r.Value);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    n = new XProcessingInstruction(r.Name, r.Value);
                    break;

                case XmlNodeType.DocumentType:
                    n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value);
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
                }
                if (n != null)
                {
                    if (baseUri != null && baseUri != uri)
                    {
                        n.SetBaseUri(uri);
                    }
                    if (li != null && li.HasLineInfo())
                    {
                        n.SetLineInfo(li.LineNumber, li.LinePosition);
                    }
                    c.AddNodeSkipNotify(n);
                    n = null;
                }
            } while (r.Read());
        }
Esempio n. 3
0
            public bool ReadContentFrom(XContainer rootContainer, XmlReader r, LoadOptions o)
            {
                XNode  newNode = null;
                string baseUri = r.BaseURI;

                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                {
                    XElement e = new XElement(_eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (_baseUri != null && _baseUri != baseUri)
                    {
                        e.SetBaseUri(baseUri);
                    }
                    if (_lineInfo != null && _lineInfo.HasLineInfo())
                    {
                        e.SetLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                    }
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            XAttribute a = new XAttribute(_aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                            if (_lineInfo != null && _lineInfo.HasLineInfo())
                            {
                                a.SetLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                            }
                            e.AppendAttributeSkipNotify(a);
                        } while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    _currentContainer.AddNodeSkipNotify(e);
                    if (!r.IsEmptyElement)
                    {
                        _currentContainer = e;
                        if (_baseUri != null)
                        {
                            _baseUri = baseUri;
                        }
                    }
                    break;
                }

                case XmlNodeType.EndElement:
                {
                    if (_currentContainer.content == null)
                    {
                        _currentContainer.content = string.Empty;
                    }
                    // Store the line info of the end element tag.
                    // Note that since we've got EndElement the current container must be an XElement
                    XElement e = _currentContainer as XElement;
                    Debug.Assert(e != null, "EndElement received but the current container is not an element.");
                    if (e != null && _lineInfo != null && _lineInfo.HasLineInfo())
                    {
                        e.SetEndElementLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                    }
                    if (_currentContainer == rootContainer)
                    {
                        return(false);
                    }
                    if (_baseUri != null && _currentContainer.HasBaseUri)
                    {
                        _baseUri = _currentContainer.parent.BaseUri;
                    }
                    _currentContainer = _currentContainer.parent;
                    break;
                }

                case XmlNodeType.Text:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                    if ((_baseUri != null && _baseUri != baseUri) ||
                        (_lineInfo != null && _lineInfo.HasLineInfo()))
                    {
                        newNode = new XText(r.Value);
                    }
                    else
                    {
                        _currentContainer.AddStringSkipNotify(r.Value);
                    }
                    break;

                case XmlNodeType.CDATA:
                    newNode = new XCData(r.Value);
                    break;

                case XmlNodeType.Comment:
                    newNode = new XComment(r.Value);
                    break;

                case XmlNodeType.ProcessingInstruction:
                    newNode = new XProcessingInstruction(r.Name, r.Value);
                    break;

                case XmlNodeType.DocumentType:
                    newNode = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value);
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
                }

                if (newNode != null)
                {
                    if (_baseUri != null && _baseUri != baseUri)
                    {
                        newNode.SetBaseUri(baseUri);
                    }

                    if (_lineInfo != null && _lineInfo.HasLineInfo())
                    {
                        newNode.SetLineInfo(_lineInfo.LineNumber, _lineInfo.LinePosition);
                    }

                    _currentContainer.AddNodeSkipNotify(newNode);
                    newNode = null;
                }

                return(true);
            }
Esempio n. 4
0
            public bool ReadContentFrom(XContainer rootContainer, XmlReader r)
            {
                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                    XElement e = new XElement(_eCache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            e.AppendAttributeSkipNotify(new XAttribute(_aCache.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value));
                        } while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    _currentContainer.AddNodeSkipNotify(e);
                    if (!r.IsEmptyElement)
                    {
                        _currentContainer = e;
                    }
                    break;

                case XmlNodeType.EndElement:
                    if (_currentContainer.content == null)
                    {
                        _currentContainer.content = string.Empty;
                    }
                    if (_currentContainer == rootContainer)
                    {
                        return(false);
                    }
                    _currentContainer = _currentContainer.parent;
                    break;

                case XmlNodeType.Text:
                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.Whitespace:
                    _currentContainer.AddStringSkipNotify(r.Value);
                    break;

                case XmlNodeType.CDATA:
                    _currentContainer.AddNodeSkipNotify(new XCData(r.Value));
                    break;

                case XmlNodeType.Comment:
                    _currentContainer.AddNodeSkipNotify(new XComment(r.Value));
                    break;

                case XmlNodeType.ProcessingInstruction:
                    _currentContainer.AddNodeSkipNotify(new XProcessingInstruction(r.Name, r.Value));
                    break;

                case XmlNodeType.DocumentType:
                    _currentContainer.AddNodeSkipNotify(new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value));
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(SR.InvalidOperation_UnresolvedEntityReference);
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(SR.Format(SR.InvalidOperation_UnexpectedNodeType, r.NodeType));
                }
                return(true);
            }
Esempio n. 5
0
        internal void ReadContentFrom(XmlReader r, LoadOptions o)
        {
            if ((o & (LoadOptions.SetLineInfo | LoadOptions.SetBaseUri)) == LoadOptions.None)
            {
                this.ReadContentFrom(r);
            }
            else
            {
                if (r.ReadState != System.Xml.ReadState.Interactive)
                {
                    throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedInteractive"));
                }
                XContainer     parent  = this;
                XNode          n       = null;
                NamespaceCache cache   = new NamespaceCache();
                NamespaceCache cache2  = new NamespaceCache();
                string         baseUri = ((o & LoadOptions.SetBaseUri) != LoadOptions.None) ? r.BaseURI : null;
                IXmlLineInfo   info    = ((o & LoadOptions.SetLineInfo) != LoadOptions.None) ? (r as IXmlLineInfo) : null;
                do
                {
                    string baseURI = r.BaseURI;
                    switch (r.NodeType)
                    {
                    case XmlNodeType.Element:
                    {
                        XElement element = new XElement(cache.Get(r.NamespaceURI).GetName(r.LocalName));
                        if ((baseUri != null) && (baseUri != baseURI))
                        {
                            element.SetBaseUri(baseURI);
                        }
                        if ((info != null) && info.HasLineInfo())
                        {
                            element.SetLineInfo(info.LineNumber, info.LinePosition);
                        }
                        if (r.MoveToFirstAttribute())
                        {
                            do
                            {
                                XAttribute a = new XAttribute(cache2.Get((r.Prefix.Length == 0) ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value);
                                if ((info != null) && info.HasLineInfo())
                                {
                                    a.SetLineInfo(info.LineNumber, info.LinePosition);
                                }
                                element.AppendAttributeSkipNotify(a);
                            }while (r.MoveToNextAttribute());
                            r.MoveToElement();
                        }
                        parent.AddNodeSkipNotify(element);
                        if (!r.IsEmptyElement)
                        {
                            parent = element;
                            if (baseUri != null)
                            {
                                baseUri = baseURI;
                            }
                        }
                        break;
                    }

                    case XmlNodeType.Text:
                    case XmlNodeType.Whitespace:
                    case XmlNodeType.SignificantWhitespace:
                        if (((baseUri == null) || (baseUri == baseURI)) && ((info == null) || !info.HasLineInfo()))
                        {
                            parent.AddStringSkipNotify(r.Value);
                        }
                        else
                        {
                            n = new XText(r.Value);
                        }
                        break;

                    case XmlNodeType.CDATA:
                        n = new XCData(r.Value);
                        break;

                    case XmlNodeType.EntityReference:
                        if (!r.CanResolveEntity)
                        {
                            throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnresolvedEntityReference"));
                        }
                        r.ResolveEntity();
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        n = new XProcessingInstruction(r.Name, r.Value);
                        break;

                    case XmlNodeType.Comment:
                        n = new XComment(r.Value);
                        break;

                    case XmlNodeType.DocumentType:
                        n = new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value, r.DtdInfo);
                        break;

                    case XmlNodeType.EndElement:
                    {
                        if (parent.content == null)
                        {
                            parent.content = string.Empty;
                        }
                        XElement element2 = parent as XElement;
                        if (((element2 != null) && (info != null)) && info.HasLineInfo())
                        {
                            element2.SetEndElementLineInfo(info.LineNumber, info.LinePosition);
                        }
                        if (parent == this)
                        {
                            return;
                        }
                        if ((baseUri != null) && parent.HasBaseUri)
                        {
                            baseUri = parent.parent.BaseUri;
                        }
                        parent = parent.parent;
                        break;
                    }

                    case XmlNodeType.EndEntity:
                        break;

                    default:
                        throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnexpectedNodeType", new object[] { r.NodeType }));
                    }
                    if (n != null)
                    {
                        if ((baseUri != null) && (baseUri != baseURI))
                        {
                            n.SetBaseUri(baseURI);
                        }
                        if ((info != null) && info.HasLineInfo())
                        {
                            n.SetLineInfo(info.LineNumber, info.LinePosition);
                        }
                        parent.AddNodeSkipNotify(n);
                        n = null;
                    }
                }while (r.Read());
            }
        }
Esempio n. 6
0
        internal void ReadContentFrom(XmlReader r)
        {
            if (r.ReadState != System.Xml.ReadState.Interactive)
            {
                throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_ExpectedInteractive"));
            }
            XContainer     parent = this;
            NamespaceCache cache  = new NamespaceCache();
            NamespaceCache cache2 = new NamespaceCache();

            do
            {
                switch (r.NodeType)
                {
                case XmlNodeType.Element:
                {
                    XElement n = new XElement(cache.Get(r.NamespaceURI).GetName(r.LocalName));
                    if (r.MoveToFirstAttribute())
                    {
                        do
                        {
                            n.AppendAttributeSkipNotify(new XAttribute(cache2.Get((r.Prefix.Length == 0) ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value));
                        }while (r.MoveToNextAttribute());
                        r.MoveToElement();
                    }
                    parent.AddNodeSkipNotify(n);
                    if (!r.IsEmptyElement)
                    {
                        parent = n;
                    }
                    break;
                }

                case XmlNodeType.Text:
                case XmlNodeType.Whitespace:
                case XmlNodeType.SignificantWhitespace:
                    parent.AddStringSkipNotify(r.Value);
                    break;

                case XmlNodeType.CDATA:
                    parent.AddNodeSkipNotify(new XCData(r.Value));
                    break;

                case XmlNodeType.EntityReference:
                    if (!r.CanResolveEntity)
                    {
                        throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnresolvedEntityReference"));
                    }
                    r.ResolveEntity();
                    break;

                case XmlNodeType.ProcessingInstruction:
                    parent.AddNodeSkipNotify(new XProcessingInstruction(r.Name, r.Value));
                    break;

                case XmlNodeType.Comment:
                    parent.AddNodeSkipNotify(new XComment(r.Value));
                    break;

                case XmlNodeType.DocumentType:
                    parent.AddNodeSkipNotify(new XDocumentType(r.LocalName, r.GetAttribute("PUBLIC"), r.GetAttribute("SYSTEM"), r.Value, r.DtdInfo));
                    break;

                case XmlNodeType.EndElement:
                    if (parent.content == null)
                    {
                        parent.content = string.Empty;
                    }
                    if (parent == this)
                    {
                        return;
                    }
                    parent = parent.parent;
                    break;

                case XmlNodeType.EndEntity:
                    break;

                default:
                    throw new InvalidOperationException(System.Xml.Linq.Res.GetString("InvalidOperation_UnexpectedNodeType", new object[] { r.NodeType }));
                }
            }while (r.Read());
        }