public static void GetParseTreeInfo(Element e, ref string msg, ref int level)
        {

            if (e == null) return;

            string tabs = "";


            for (int i = 0; i < level; i++)
                tabs += "\t";

            msg += tabs;

            msg += "Element " + e.Name + ": " + e.Value + "\n";
            foreach (Libellus.XML.Attribute a in e.Attributes)
            {
                msg += tabs + "\tAttribute " + a.Name + ": " + a.Value + "\n";
            }


            if (e.Children.Count == 0)
            {
                return;
            }
            else
            {
                level++;
                foreach(Element child in e.Children)
                {
                    GetParseTreeInfo(child, ref msg, ref level);
                }
                level--;
            }
            
        }
        /*
        public List<Element> GetChildElement(string name)
        {
            List<Element> l = new List<Element>();
            foreach(Element e in this.Children)
            {
                if (e.Name.Equals(name))
                    l.Add(e);
            }

            return l;
        }
        */
        public void AddChildElement(Element e)
        {
            this.Children.Add(e);
        }
        private void FindChildElementsHelper(List<Element> list, Element e, string name)
        {
            if (e.Children.Count == 0)
                return;

            foreach (Element child in e.Children)
            {
                if (child.Name.Equals(name))
                {
                    list.Add(child);
                }

                FindChildElementsHelper(list, child, name);
            }
        }
        /// <summary>
        /// Receives a HashTable of Element objects that is received
        /// from the XMLParser class, breaks down the Elements that are relevant,
        /// and fills necessary text boxes
        /// </summary>
        /// <param name="table"></param>
        public Constants.XMLResultReturnValue FillFromXMLResults(Element element)
        {
            Element bookListElement = element.GetFirstElementOf("BookList");
            Element bookDataElement = element.GetFirstElementOf("BookData");
            Element titleElement = element.GetFirstElementOf("Title");
            Element titleLongElement = element.GetFirstElementOf("TitleLong");
            Element authorsTextElement = element.GetFirstElementOf("Authors");
            Element authorElement = element.GetFirstElementOf("AuthorsText");
            Element publisherTextElement = element.GetFirstElementOf("PublisherText");
            Element detailsElement = element.GetFirstElementOf("Details");
            Element summaryElement = element.GetFirstElementOf("Summary");
            Element notesElement = element.GetFirstElementOf("Notes");
            Element awardsTextElement = element.GetFirstElementOf("AwardsText");
            Element urlsTextElement = element.GetFirstElementOf("UrlsText");
            Element editorsElement = element.GetFirstElementOf("EditorsText");
            Element subjectsElement = element.GetFirstElementOf("Subjects");
            Element pricesElement = element.GetFirstElementOf("Prices");

            if (bookListElement.GetAttribute("total_results").Equals("0"))
                return Constants.XMLResultReturnValue.NOT_FOUND; 
           
            this.Id = bookDataElement.GetAttribute("book_id");
            this.ISBN = bookDataElement.GetAttribute("isbn");
            this.ShortTitle = titleElement.Value;
            this.LongTitle = titleLongElement.Value;
            this.PublisherInfo = publisherTextElement.Value;
            this.PublisherId = publisherTextElement.GetAttribute("publisher_id");
            this.PhysicalDescription = detailsElement.GetAttribute("physical_description_text");
            this.Language = detailsElement.GetAttribute("language");
            this.Dewey = detailsElement.GetAttribute("dewey_decimal");
            this.DeweyNormalized = detailsElement.GetAttribute("dewey_decimal_normalized");
            this.Edition = detailsElement.GetAttribute("edition_info");
            this.LibraryOfCongress = detailsElement.GetAttribute("lcc_number");
            this.Summary = summaryElement.Value;
            this.Notes = notesElement.Value;
            //do subject info
            this.Urls = urlsTextElement.Value;
            this.Awards = awardsTextElement.Value;

            //if there isn't any person elements, we do the best we can with what we have
            if (authorsTextElement.Children.Count == 0 && !authorElement.Value.Equals(""))
            {
                Person p = new Person();
                p.FirstName = Person.GetFirstNameFromPersonValue(authorElement.Value);
                p.LastName = Person.GetLastNameFromPersonValue(authorElement.Value);
                p.Id = Utils.CreateIdFromString(authorElement.Value);
                this.Authors.Add(p);
            }
            else
            {
                foreach (Element child in authorsTextElement.FindChildElements("Person"))
                {
                    Person p = new Person();
                    p.FillFromElement(child);
                    this.Authors.Add(p);
                }
            }

            if (editorsElement.Children.Count == 0 && !editorsElement.Value.Equals(""))
            {
                Person p = new Person();
                p.FirstName = Person.GetFirstNameFromPersonValue(editorsElement.Value);
                p.LastName = Person.GetLastNameFromPersonValue(editorsElement.Value);
                p.Id = Utils.CreateIdFromString(editorsElement.Value);
                this.Editors.Add(p);
            }
            else
            {
                foreach (Element child in editorsElement.FindChildElements("Person"))
                {
                    Person p = new Person();
                    p.FillFromElement(child);
                    this.Editors.Add(p);
                }
            }

            foreach (Element subject in subjectsElement.FindChildElements("Subject"))
            {
                Subject s = new Subject();
                s.Id = subject.GetAttribute("subject_id");
                s.Name = subject.Value;
                this.Subjects.Add(s);
            }

            int newCount = 0;
            int usedCount = 0;
            double newTotal = 0.0;
            double usedTotal = 0.0;
            foreach (Element price in pricesElement.FindChildElements("Price"))
            {
                string val = price.GetAttribute("price");
                string isNew = price.GetAttribute("is_new");
                if(isNew.Equals("1"))
                {
                    try
                    {
                        newTotal += Double.Parse(val);
                        newCount++;
                    }
                    catch(Exception e)
                    {
                    	Log.Instance.Out(e);
                    }
                }
                else if (isNew.Equals("0"))
                {
                    try
                    {
                        usedTotal += Double.Parse(val);
                        usedCount++;
                    }
                    catch (Exception e)
                    {
                    	Log.Instance.Out(e);
                    }
                }
            }
            double newAvg = newTotal / newCount;
            double usedAvg = usedTotal / usedCount;
          
            this.NewPrice = newAvg.ToString("C");
            this.UsedPrice = usedAvg.ToString("C");

            if (this.NewPrice.Contains("NaN"))
                this.NewPrice = "";

            if (this.UsedPrice.Contains("NaN"))
                this.UsedPrice = "";

            return Constants.XMLResultReturnValue.SUCCESS;
        }
 public void FillFromElement(Element e)
 {
     this.FirstName = Person.GetFirstNameFromFull(e.Value);
     this.LastName = Person.GetLastNameFromFull(e.Value);
     this.Id = e.GetAttribute("person_id");
 }
        public static Element ParseXML(Stream xmlStream)
        {
            StreamReader textReader = new StreamReader(xmlStream);
            string responseString = "";
            while (!textReader.EndOfStream)
                responseString += textReader.ReadLine();

            StringReader stringReader = new StringReader(responseString);
            
            XmlTextReader reader = new XmlTextReader(stringReader);

#if DEBUG
            Console.Out.WriteLine("Original XML:");
            responseString = responseString.Replace(">", ">\n");
            Console.Out.WriteLine(responseString);
            Console.Out.WriteLine("");
#endif
            Element rootNode = new Element();
            rootNode.IsRootNode = true;
            //int lastDepth = -1;
            try
            {
                Stack<Element> stack = new Stack<Element>();
                
                stack.Push(rootNode);

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            Element newElement = new Element();
                            newElement.Name = reader.Name;
                            newElement.Parent = stack.Peek();
                            stack.Peek().AddChildElement(newElement);
                            while (reader.MoveToNextAttribute())
                            {
                                Libellus.XML.Attribute a = new Libellus.XML.Attribute();
                                a.Name = reader.Name;
                                a.Value = reader.ReadContentAsString();
                                newElement.Attributes.Add(a);
                            }
                            if(!newElement.Name.Equals("Price"))
                                stack.Push(newElement);

                            break;

                        case XmlNodeType.Text:
                            stack.Peek().Value = reader.Value;
                            break;

                        case XmlNodeType.EndElement:
                            stack.Pop();
                            break;

                       
                    }
                }

                
            }
            catch (Exception e1)
            {
                ExceptionHandler.HandleException(e1);
            }

            return rootNode;
        }