Click here to Skip to main content
15,879,326 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 4: Space elevator

Rate me:
Please Sign up or sign in to vote.
4.56/5 (6 votes)
14 Aug 20066 min read 36.6K   2.2K   37  
An article on framework applications to the space elevator.
using System;
using System.Collections;
using System.Text;
using System.Xml;
using System.Windows.Forms;

namespace DataWarehouse
{
    public class DatabaseNode : TreeNode, IComparer
    {
        private int id;
        private string guid;
        private XmlElement e;
        private ArrayList children = new ArrayList();
        private string description;
        private string textOut;
        private ArrayList childNodes;
        public DatabaseNode(XmlElement e, string tagName, string idAttr, string nameAttr, string childTag)
        {
            if (nameAttr != null)
            {
                this.e = e;
                string s = e.Attributes[idAttr].Value;
                Text = e.Attributes[nameAttr].Value;
                textOut = Text;
                description = e.GetElementsByTagName("Description")[0].InnerText;
                try
                {
                    id = Int32.Parse(s);
                }
                catch (Exception)
                {
                    guid = s;
                }
            }
            XmlNodeList list = e.GetElementsByTagName(childTag);
            foreach (XmlElement node in list)
            {
                if (node.ParentNode == e)
                {
                    children.Add(node);
                }
            }
            XmlNodeList ch = e.GetElementsByTagName(tagName);
            childNodes = new ArrayList();
            foreach (XmlElement node in ch)
            {
                if (node.ParentNode != e)
                {
                    continue;
                }
                TreeNode child = new DatabaseNode(node, tagName, idAttr, nameAttr, childTag);
                childNodes.Add(child);
            }
            childNodes.Sort(this);
            foreach (TreeNode child in childNodes)
            {
                Nodes.Add(child);
            }
        }

        public DatabaseNode(DatabaseNode parent, string id, string name, string description)
        {
            guid = id;
            Text = name + "";
            this.description = description + "";
            for (int i = 0; i < parent.Nodes.Count; i++)
            {
                if (name.CompareTo(parent.Nodes[i].Text) < 0)
                {
                    parent.Nodes.Insert(i, (this));
                    return;
                }
            }
            parent.Nodes.Add(this);
        }
 
        public void AddChild(string id, string name, string description, string ext, string length)
        {
            XmlDocument doc = e.OwnerDocument;
            XmlElement ep = doc.CreateElement("Binary");
            XmlAttribute aid = doc.CreateAttribute("BinaryId");
            aid.Value = id;
            ep.Attributes.Append(aid);
            XmlAttribute aName = doc.CreateAttribute("BinaryName");
            aName.Value = name;
            ep.Attributes.Append(aName);
            XmlElement d = doc.CreateElement("Description");
            d.InnerText = description;
            ep.AppendChild(d);
            XmlAttribute aParent = doc.CreateAttribute("ParentId");
            aParent.Value = UID + "";
            ep.Attributes.Append(aParent);
            XmlAttribute aExt = doc.CreateAttribute("Ext");
            aExt.Value = ext;
            ep.Attributes.Append(aExt);
            e.AppendChild(ep);

        }

        public int Id
        {
            get
            {
                return id;
            }
        }

        public string UID
        {
            get
            {
                return guid;
            }
        }

        public int Count
        {
            get
            {
                return children.Count;
            }
        }

        public XmlElement Element
        {
            get
            {
                return e;
            }
        }

        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value + "";
            }
        }

        public int ChildrenCount
        {
            get
            {
                return children.Count;
            }
        }

        public XmlElement GetChild(int i)
        {
            return children[i] as XmlElement;
        }

        public DatabaseNode this[int i]
        {
            get
            {
                if (i >= Count | i < 0)
                {
                    return null;
                }
                return childNodes[i] as DatabaseNode;
            }
        }

        public void RemoveChild(XmlElement e)
        {
            children.Remove(e);
        }

        public int Compare(object o1, object o2)
        {
            DatabaseNode n1 = o1 as DatabaseNode;
            DatabaseNode n2 = o2 as DatabaseNode;
            return n1.Text.CompareTo(n2.Text);
        }

        public string GetChildName(int i)
        {
            XmlElement el = children[i] as XmlElement;
            return el.Attributes["BinaryName"].Value;
        }

        public string GetChildUID(int i)
        {
            XmlElement el = children[i] as XmlElement;
            return el.Attributes["BinaryID"].Value;
        }

        public ArrayList List
        {
            get
            {
                ArrayList l = new ArrayList();
                getList(l, 0);
                return l;
            }
        }



        static public implicit operator XmlElement(DatabaseNode node)
        {
            return node.e;
        }

        private void getList(ArrayList list, int level)
        {
            list.Add(new object[] { this, level });
            for (int i = 0; i < Count; i++)
            {
                this[i].getList(list, level + 1);
            }
        }

        public void Remove(XmlElement e)
        {
            children.Remove(e);
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions