public XmlElementContext(SerializationContext original, XDeclaration?declaration,
                                 XNamespace?xNamespace, string rootName) : base(original)
        {
            XDocument?doc = null;
            XElement? ele = null;

            switch (Argument.NotNull(original, nameof(original)).SerializerMode)
            {
            case SerializerMode.Deserialize:
                doc = XDocument.Load(TextReader, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
                break;

            case SerializerMode.Serialize:
                if (declaration != null)
                {
                    doc             = new XDocument(declaration, xNamespace != null ? new XElement(xNamespace + rootName) : new XElement(rootName));
                    _currentElement = doc;
                }
                else
                {
                    ele             = new XElement(rootName);
                    _currentElement = ele;
                }

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(original));
            }

            XElement = (ele ?? doc?.Root) ?? throw new InvalidOperationException("XElement is Null");
        }
Beispiel #2
0
        public override bool MoveToNext()
        {
            XNode?currentNode = _source as XNode;

            if (currentNode != null)
            {
                XContainer?container = currentNode.GetParent();
                if (container != null)
                {
                    XNode?next;
                    for (XNode node = currentNode; node != null; node = next)
                    {
                        next = node.NextNode;
                        if (next == null)
                        {
                            break;
                        }
                        if (IsContent(container, next) && !(node is XText && next is XText))
                        {
                            _source = next;
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #3
0
        public override bool MoveToNext(XPathNodeType type)
        {
            XNode?currentNode = _source as XNode;

            if (currentNode != null)
            {
                XContainer?container = currentNode.GetParent();
                if (container != null)
                {
                    int mask = GetElementContentMask(type);
                    if ((TextMask & mask) != 0 && container.GetParent() == null && container is XDocument)
                    {
                        mask &= ~TextMask;
                    }
                    XNode?next;
                    for (XNode node = currentNode; ; node = next)
                    {
                        next = node.NextNode;
                        if (next == null)
                        {
                            break;
                        }
                        if (((1 << (int)next.NodeType) & mask) != 0 && !(node is XText && next is XText))
                        {
                            _source = next;
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #4
0
        public override bool MoveToPrevious()
        {
            XNode?currentNode = _source as XNode;

            if (currentNode != null)
            {
                XContainer?container = currentNode.GetParent();
                if (container != null)
                {
                    XNode?previous = null;
                    foreach (XNode node in container.Nodes())
                    {
                        if (node == currentNode)
                        {
                            if (previous != null)
                            {
                                _source = previous;
                                return(true);
                            }
                            return(false);
                        }

                        if (IsContent(container, node))
                        {
                            previous = node;
                        }
                    }
                }
            }
            return(false);
        }
Beispiel #5
0
        public override void WriteFullEndElement()
        {
            XElement e = (XElement)_parent !;

            if (e.IsEmpty)
            {
                e.Add(string.Empty);
            }
            _parent = e.parent;
        }
Beispiel #6
0
        public static XContainer?GetParent(this XObject obj)
        {
            XContainer?ret = (XContainer?)obj.Parent ?? obj.Document;

            if (ret == obj)
            {
                return(null);
            }
            return(ret);
        }
Beispiel #7
0
        public override XmlReader ReadSubtree()
        {
            XContainer?c = _source as XContainer;

            if (c == null)
            {
                throw new InvalidOperationException(SR.Format(SR.InvalidOperation_BadNodeType, NodeType));
            }
            return(c.CreateReader());
        }
        private void ExtractFeatures(XContainer?layer)
        {
            if (layer == null)
            {
                return;
            }

            foreach (var feature in layer.Elements())
            {
                _featureInfo?.FeatureInfos?.Add(ExtractFeatureElements(feature));
            }
        }
Beispiel #9
0
        private void AddNode(XNode n)
        {
            if (_parent != null)
            {
                _parent.Add(n);
            }
            else
            {
                Add(n);
            }
            XContainer?c = n as XContainer;

            if (c != null)
            {
                _parent = c;
            }
        }
Beispiel #10
0
        protected override void Dispose(bool disposing)
        {
            if (Original.SerializerMode != SerializerMode.Serialize)
            {
                return;
            }

            using (var writer = XmlWriter.Create(TextWriter, new XmlWriterSettings {
                Indent = true, NamespaceHandling = NamespaceHandling.OmitDuplicates
            }))
            {
                _currentElement?.WriteTo(writer);
                _currentElement = null;
            }

            base.Dispose(disposing);
        }
Beispiel #11
0
        public override bool MoveToFirstChild()
        {
            XContainer?container = _source as XContainer;

            if (container != null)
            {
                foreach (XNode node in container.Nodes())
                {
                    if (IsContent(container, node))
                    {
                        _source = node;
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #12
0
        public override bool MoveToChild(string localName, string namespaceName)
        {
            XContainer?c = _source as XContainer;

            if (c != null)
            {
                foreach (XElement element in c.Elements())
                {
                    if (element.Name.LocalName == localName &&
                        element.Name.NamespaceName == namespaceName)
                    {
                        _source = element;
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #13
0
 private static IEnumerable <XElement> GetDescendants <T>(IEnumerable <T?> source, XName?name, bool self) where T : XContainer
 {
     foreach (XContainer?root in source)
     {
         if (root != null)
         {
             if (self)
             {
                 XElement e = (XElement)root;
                 if (name == null || e.name == name)
                 {
                     yield return(e);
                 }
             }
             XNode?     n = root;
             XContainer?c = root;
             while (true)
             {
                 if (c != null && c.content is XNode)
                 {
                     n = ((XNode)c.content).next;
                 }
                 else
                 {
                     while (n != null && n != root && n == n.parent !.content)
                     {
                         n = n.parent;
                     }
                     if (n == null || n == root)
                     {
                         break;
                     }
                     n = n.next;
                 }
                 XElement?e = n as XElement;
                 if (e != null && (name == null || e.name == name))
                 {
                     yield return(e);
                 }
                 c = e;
             }
         }
     }
 }
Beispiel #14
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);
        }
Beispiel #15
0
        public override bool MoveToChild(XPathNodeType type)
        {
            XContainer?c = _source as XContainer;

            if (c != null)
            {
                int mask = GetElementContentMask(type);
                if ((TextMask & mask) != 0 && c.GetParent() == null && c is XDocument)
                {
                    mask &= ~TextMask;
                }
                foreach (XNode node in c.Nodes())
                {
                    if (((1 << (int)node.NodeType) & mask) != 0)
                    {
                        _source = node;
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #16
0
 private static IEnumerable <XNode> GetDescendantNodes <T>(IEnumerable <T?> source, bool self) where T : XContainer
 {
     foreach (XContainer?root in source)
     {
         if (root != null)
         {
             if (self)
             {
                 yield return(root);
             }
             XNode?n = root;
             while (true)
             {
                 XContainer?c = n as XContainer;
                 XNode?     first;
                 if (c != null && (first = c.FirstNode) != null)
                 {
                     n = first;
                 }
                 else
                 {
                     while (n != null && n != root && n == n.parent !.content)
                     {
                         n = n.parent;
                     }
                     if (n == null || n == root)
                     {
                         break;
                     }
                     n = n.next !;
                 }
                 yield return(n);
             }
         }
     }
 }
Beispiel #17
0
 public override void WriteEndElement()
 {
     _parent = ((XElement)_parent !).parent;
 }