using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SCJMapper_V2.CryXMLlib { /// /// Processes a CryXmlNodeRef and reports the node and its childs as XML string list /// public class XmlTree { private List m_doc = new List( ); // internal list of strings /// /// Return the derived XML text as List of strings /// public List XML_list { get { return m_doc; } } /// /// Return the derived XML text as string /// public string XML_string { get { string xml = ""; string CR = string.Format("\n"); foreach ( string s in m_doc ) xml += ( s + CR ); return xml; } } /// /// Processes all attributed of a node /// /// The node to process /// an string containing all attributes in XML compatible format ( key="value" ) private string DoAttribute( CryXmlNodeRef nodeRef ) { IXmlNode node = nodeRef; string xml = ""; // collect all attributes into one line for ( int ac=0; ac < node.getNumAttributes( ); ac++ ) { string key = ""; string value = ""; node.getAttributeByIndex(ac, out key, out value ); xml += string.Format( " {0}=\"{1}\" ", key, value); } return xml; } /// /// Processes a node and it's children /// /// The node to process /// The depth of the node to indent the XML text private void DoNode( CryXmlNodeRef nodeRef, int level ) { string tabs = "".PadRight(level, '\t'); // level is depth - will be left padded with Tabs on Add() string xml = ""; IXmlNode node = nodeRef; // first gather and handle the attributes string attr = DoAttribute( nodeRef ); xml = string.Format( "<{0} {1} ", node.getTag( ), attr ); // then do some formatting dependent of child nodes to be printed if ( node.getChildCount( ) < 1 ) { // no child - close with end tag immediately xml += string.Format( "/>" ); m_doc.Add( tabs + xml ); // add line w/o NL } else { // with children - close tag only xml += string.Format( ">" ); m_doc.Add( tabs + xml ); // add line w/o NL // do the children for ( int cc=0; cc < node.getChildCount( ); cc++ ) { CryXmlNodeRef childRef = node.getChild( cc ); DoNode( childRef, level+1 ); // recursion } xml = string.Format( "\n", node.getTag( ) ); m_doc.Add( tabs + xml ); // add line with NL to space them } } /// /// Processes a CryXmlNodeRef to derive the XML formatted structure /// Note: the created XML text can be retrieved through the XML property of this object /// /// The node to start from public void BuildXML( CryXmlNodeRef rootRef ) { m_doc.Clear( ); DoNode( rootRef, 0 ); } } }