using System.Collections; using System.Collections.Generic; using System.Text.RegularExpressions; namespace Rokojori { public class XMLDocument:XMLNode { XMLElementNode _documentElement; List _roots = new List(); // public List roots => _roots; public void AppendChild( XMLNode node ) { _roots.Add( node ); if ( XMLNode.NodeType.Element == node.nodeType ) { _documentElement = (XMLElementNode) node; } node._SetParent( this ); } public int numRoots => _roots.Count; public XMLNode GetChild( int index ) { return _roots[ index ]; } public XMLDocument():base( null, XMLNode.NodeType.Comment ) { _document = this; } public XMLElementNode documentElement { get => _documentElement; set { _documentElement = value; } } public string Serialize() { var serializer = new XMLSerializer(); return serializer.SerializeXMLNode( document ); } public static XMLDocument CreateHtmlDocument( bool addHeadAndBody = true ) { var htmlDocument = new XMLDocument(); htmlDocument._documentElement = HTMLElementName.html.Create( htmlDocument ); htmlDocument._roots.Add( htmlDocument._documentElement ); if ( addHeadAndBody ) { htmlDocument._documentElement.AppendChild( HTMLElementName.head.Create( htmlDocument ) ); htmlDocument._documentElement.AppendChild( HTMLElementName.body.Create( htmlDocument ) ); } return htmlDocument; } public XMLElementNode Create( XMLElementNodeName nodeName, string text = null ) { var element = nodeName.Create( this ); if ( text != null ) { element.AddText( text ); } return element; } public XMLTextNode CreateText( string text ) { return new XMLTextNode( this, text ); } public XMLElementNode querySelector( XMLElementSelector selector ) { return XMLQuery.SelectElement( this, selector ); } public XMLElementNode querySelector( string selector ) { return querySelector( XMLElementSelectors.From( selector ) ); } public List querySelectorAll( XMLElementSelector selector ) { return XMLQuery.SelectAllElements( this, selector ); } public List querySelectorAll( string selector ) { return querySelectorAll( XMLElementSelectors.From( selector ) ); } } }