コード例 #1
0
ファイル: ElementShowForm.cs プロジェクト: naccib/dotElements
        private void BGWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.returnElement = AgilityParser.Parse(SourceCode);
            this.toolStripProgressBar1.Value = 100;
            this.toolStripStatusLabel1.Text  = "Ready";

            foreach (Label lbl in this.groupBox2.Controls.OfType <Label>())
            {
                lbl.Visible = true;
            }

            nameLabel.Text           = this.Text = returnElement.Name;
            symbolLabel.Text         = returnElement.Symbol;
            ANumberLabel.Text        = returnElement.ANumber;
            AMassLabel.Text          = returnElement.AMass;
            MPLabel.Text             = returnElement.MP;
            BPLabel.Text             = returnElement.BP;
            NoPELabel.Text           = returnElement.NoPE;
            DensityLabel.Text        = returnElement.Density + "³";
            ColorLabel.Text          = returnElement.Color;
            CSLabel.Text             = returnElement.CrystalGeo;
            ClassificationLabel.Text = returnElement.Classification;
            groupBox2.Text           = string.Format("{0} ({1})", returnElement.Name, returnElement.Symbol.Replace(" ", ""));
        }
コード例 #2
0
        public static Elemento Parse(string HTML)
        {
            string[] HTMLArray = HTML.Split(new char[] { '\n' });
            Elemento elemento  = new Elemento();

            Logger.LogData("HTML Scrapping Started");

            foreach (string line in HTMLArray)
            {
                if (line.Contains("Name:"))
                {
                    elemento.Name = ReplaceAll(line);
                }

                if (line.Contains("Symbol:"))
                {
                    elemento.Symbol = ReplaceAll(line);
                }

                if (line.Contains("Atomic Number:"))
                {
                    elemento.ANumber = ReplaceAll(line);
                }

                if (line.Contains("Atomic Mass:"))
                {
                    elemento.AMass = ReplaceAll(line);
                }

                if (line.Contains("Melting Point:"))
                {
                    elemento.MP = ReplaceAll(line).Replace("deg;", "º");
                }

                if (line.Contains("Boiling Point:"))
                {
                    elemento.BP = ReplaceAll(line).Replace("deg;", "º");
                }

                if (line.Contains("Number of Protons/Electrons:"))
                {
                    elemento.NoPE = ReplaceAll(line);
                }

                if (line.Contains("Number of Neutrons:"))
                {
                    elemento.NoN = ReplaceAll(line);
                }

                if (line.Contains("Crystal Structure:"))
                {
                    elemento.CrystalGeo = ReplaceAll(line);
                }

                if (line.Contains("Density @"))
                {
                    elemento.Density = ReplaceAll(line);
                }

                if (line.Contains("Color:"))
                {
                    elemento.Color = ReplaceAll(line);
                }

                if (line.Contains("Classification:"))
                {
                    if (line.Contains("Non-metal"))
                    {
                        elemento.Classification = "Non-Metal";
                    }
                    else
                    {
                        elemento.Classification = "Metal";
                    }
                }

                if (line.Contains("Number of Energy Levels:"))
                {
                    Int32.TryParse(line.Replace("<br><strong>Number of Energy Levels:</strong> ", ""), out elemento.EnergyLVLNumber);
                    //MessageBox.Show(elemento.EnergyLVLNumber.ToString());
                }

                try
                {
                    if (line.Contains("First Energy Level:"))
                    {
                        elemento.EnergyLevels[0] = line.Replace("<br><strong>First Energy Level:</strong> ", "");
                    }

                    if (line.Contains("Second Energy Level:"))
                    {
                        elemento.EnergyLevels[1] = line.Replace("<br><strong>Second Energy Level:</strong> ", "");
                    }

                    if (line.Contains("Third Energy Level:"))
                    {
                        elemento.EnergyLevels[2] = line.Replace("<br><strong>Third Energy Level:</strong> ", "");
                    }

                    if (line.Contains("Fourth Energy Level:"))
                    {
                        elemento.EnergyLevels[3] = line.Replace("<br><strong>Fourth Energy Level:</strong> ", "");
                    }

                    if (line.Contains("Fifth Energy Level:"))
                    {
                        elemento.EnergyLevels[4] = line.Replace("<br><strong>Fifth Energy Level:</strong> ", "");
                    }

                    if (line.Contains("Sixth Energy Level:"))
                    {
                        elemento.EnergyLevels[5] = line.Replace("<br><strong>Sixth Energy Level:</strong> ", "");
                    }

                    if (line.Contains("Seventh Energy Level:"))
                    {
                        elemento.EnergyLevels[6] = line.Replace("<br><strong>Seventh Energy Level:</strong> ", "");
                    }
                }
                catch (Exception x)
                {
                    Logger.LogData("Trivial ERROR: " + x.Message);
                    //MessageBox.Show("An Error Ocurred :[\n\n\n\nDetails: " + x.Message, "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }

            // ler html
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(HTML);

            List <string> tempList = new List <string>();

            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//font"))
            {
                if (node.InnerText != "Isotope" && node.InnerText != "Half Life" && node.InnerText.Length < 15)
                {
                    tempList.Add(node.InnerText);
                }
            }

            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//img"))
            {
                if (node.GetAttributeValue("ALT", "error").Contains("[Bohr Model of"))
                {
                    elemento.AtomicStructureImage = "http://www.chemicalelements.com/" + node.GetAttributeValue("SRC", "error").Replace("..", "");
                }
            }

            for (int i = 0; i < tempList.Count / 2; ++i)
            {
                if (i % 2 == 0)
                {
                    elemento.Isotopes[tempList[i]] = tempList[i + 1];
                }
                else
                {
                    elemento.Isotopes[tempList[i + 1]] = tempList[i];
                }
            }

            // MessageBox.Show(elemento.GetBasicElemString());
            // MessageBox.Show(elemento.GetIsotopes());


            Logger.LogData(elemento.GetBasicElemString());
            Logger.LogData("END OF PROCESSING", true);

            return(elemento);
        }