Ejemplo n.º 1
0
        private static bool WriteStartTag(XmlWriter writer, XmlNamespaceManager manager, Element element, out string ns)
        {
            ns = null;

            // Custom elements take priority over component
            if (element is ICustomElement customElement)
            {
                customElement.CreateStartElement(writer);
                if (!customElement.ProcessChildren)
                {
                    return(false); // Don't need to do any more work.
                }
            }
            else
            {
                XmlComponent component = KmlFactory.FindType(element.GetType());
                if (component == null)
                {
                    // We can't handle it so ignore it
                    System.Diagnostics.Debug.WriteLine("Unknown Element type - please register first." + element.GetType());
                    return(false);
                }

                ns = component.NamespaceUri;
                string prefix = FindPrefix(manager, ns);
                writer.WriteStartElement(prefix, component.Name, component.NamespaceUri);
            }

            return(true);
        }
Ejemplo n.º 2
0
        private static void SerializeElement(XmlWriter writer, Element element)
        {
            // Write start tag
            XmlComponent component = KmlFactory.FindType(element.GetType());

            // Custom elements take priority over component
            ICustomElement customElement = element as ICustomElement;

            if (customElement != null)
            {
                customElement.CreateStartElement(writer);
                if (!customElement.ProcessChildren)
                {
                    return; // Don't need to to any more work.
                }
            }
            else if (component != null)
            {
                writer.WriteStartElement(component.Name, component.NamespaceUri);
            }
            else
            {
                // We can't handle it so ignore it
                System.Diagnostics.Debug.WriteLine("Unknown Element type - please register first." + element.GetType());
                return; // Skip
            }

            // Write the attributes - unknown, serialized then namespaces.
            foreach (var att in element.Attributes)
            {
                writer.WriteAttributeString(att.Prefix, att.Name, att.NamespaceUri, att.Value);
            }

            WriteAttributes(writer, element);

            foreach (var ns in element.Namespaces.GetNamespacesInScope(XmlNamespaceScope.ExcludeXml))
            {
                writer.WriteAttributeString("xmlns", ns.Key, string.Empty, ns.Value);
            }

            // Now the text part
            WriteData(writer, element.InnerText);

            // Now write the elements - serialized, children then unknown children.
            WriteElements(writer, element);
            SerializeElements(writer, element.OrderedChildren);
            SerializeElements(writer, element.Orphans);

            // Finished...
            writer.WriteEndElement();
        }
 private void SerializeExtensions(Element element, Type elementType)
 {
     foreach (Type type in KmlFactory.GetKnownExtensionTypes(elementType))
     {
         foreach (Element orphan in element.Orphans)
         {
             // Make sure we don't serialize the same element twice
             // by adding it to the set
             if ((orphan.GetType() == type) && this.serializedElements.Add(orphan))
             {
                 this.serializer.SerializeElement(this.writer, this.manager, orphan);
             }
         }
     }
 }
Ejemplo n.º 4
0
        private Element CreateElement()
        {
            if (this.reader.Depth > MaxNestingDepth)
            {
                throw new InvalidOperationException("Maximum nesting depth has been reached.");
            }

            if (this.reader.NodeType != XmlNodeType.Element)
            {
                return(null);
            }

            // Need to check this here before we move to the attributes,
            // as reader.NodeType will never be EndElement for empty elements
            // and when we move to an attribute, IsEmptyElement doesn't work
            this.currentElementIsEmpty = this.reader.IsEmptyElement;

            Element element = KmlFactory.CreateElement(this.GetXmlComponent());

            return(element ?? new UnknownElement(new XmlComponent(this.reader)));
        }
Ejemplo n.º 5
0
        private Element GetElement()
        {
            if (_reader.Depth > MaxNestingDepth)
            {
                throw new InvalidOperationException("Maximum nesting depth has been reached.");
            }

            if (_reader.NodeType != XmlNodeType.Element)
            {
                return(null);
            }

            // Need to check this here before we move to the attributes,
            // as reader.NodeType will never be EndElement for empty elements
            // and when we move to an attribute, IsEmptyElement doesn't work
            bool isEmpty = _reader.IsEmptyElement;

            Element parent = KmlFactory.CreateElement(this.GetXmlComponent());

            if (parent == null)
            {
                parent = new UnknownElement(new XmlComponent(_reader));
            }
            else if (parent is IHtmlContent)
            {
                this.ProcessAttributes(parent);

                // No need to process all the children
                string text = string.Empty;
                if (!isEmpty) // Is there something to parse?
                {
                    text = XmlExtractor.FlattenXml(_reader);
                }

                ((IHtmlContent)parent).Text = text;
                return(parent);
            }

            this.ProcessAttributes(parent); // Empties can have attributes though

            if (!isEmpty)                   // Is there any text/children to process?
            {
                while (_reader.Read())
                {
                    if (_reader.NodeType == XmlNodeType.EndElement)
                    {
                        break;
                    }

                    switch (_reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        this.AddChild(parent);
                        break;

                    case XmlNodeType.CDATA:     // Treat like normal text
                    case XmlNodeType.Text:
                        parent.AddInnerText(_reader.Value);
                        break;
                    }
                }
            }
            return(parent);
        }