/// <summary> /// Extracts the inner XML of the XmlReader, without adding additional /// namespaces. /// </summary> /// <param name="reader">The XmlReader to extract data from.</param> /// <returns> /// A string representing the inner XML of the current XML node. /// </returns> public static string FlattenXml(XmlReader reader) { var instance = new XmlExtractor(reader); instance.ProcessChild(); return(instance.xml.ToString()); }
private void PopulateElement(Element element) { if (element == null) { return; } this.ProcessAttributes(element); if (element is IHtmlContent htmlContent) { if (this.currentElementIsEmpty) { htmlContent.Text = string.Empty; } else { htmlContent.Text = XmlExtractor.FlattenXml(this.reader); } } else if (!this.currentElementIsEmpty) { while (this.reader.Read()) { switch (this.reader.NodeType) { case XmlNodeType.Element: this.AddChild(element); break; case XmlNodeType.EndElement: return; case XmlNodeType.CDATA: // Treat like normal text case XmlNodeType.Text: element.AddInnerText(this.reader.Value); break; } } } }
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); }
/// <summary> /// Extracts the inner XML of the XmlReader, without adding additional /// namespaces. /// </summary> /// <param name="reader">The XmlReader to extract data from.</param> /// <returns> /// A string representing the inner XML of the current XML node. /// </returns> public static string FlattenXml(XmlReader reader) { XmlExtractor instance = new XmlExtractor(reader); instance.ProcessChild(); return instance._xml.ToString(); }