Пример #1
0
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // create the dialog
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.InitialDirectory = @"C:\\";
            dlg.Filter           = "XML Files|*.xml";
            dlg.FilterIndex      = 2;
            dlg.RestoreDirectory = true;

            if (DialogResult.OK == dlg.ShowDialog())
            {
                // if clicked clear everything
                lst_AFC.Items.Clear();
                lst_NFC.Items.Clear();

                XmlReaderSettings settings = new XmlReaderSettings();
                settings.ConformanceLevel = ConformanceLevel.Document;

                // we want to make sure our reader gates only the XML
                // so we'll set it to ignore comments and whitespace
                settings.IgnoreComments   = true;
                settings.IgnoreWhitespace = true;

                // using the XmlReader
                using (XmlReader reader = XmlReader.Create(dlg.FileName, settings))
                {
                    // skip the metadata
                    reader.MoveToContent();

                    // verify that this is our stock data
                    if (reader.Name != "NFL_Football_Teams")
                    {
                        // return that this is not right
                        MessageBox.Show("These are not the data you're looking for.");
                        return;
                    }

                    // if it is, move through the data and get what we want
                    while (reader.Read())
                    {
                        // variable to hold retrieved data
                        string name     = "";
                        string city     = "";
                        string division = "";
                        if (reader.Name == "Team_Name" && reader.IsStartElement())
                        {
                            name = reader.ReadElementContentAsString();
                        }
                        if (reader.Name == "City" && reader.IsStartElement())
                        {
                            city = reader.ReadElementContentAsString();
                        }
                        if (reader.Name == "Division" && reader.IsStartElement())
                        {
                            division = reader.ReadElementContentAsString();
                        }

                        // creates a team then adds to appropriate list
                        if (division == "AFC")
                        {
                            FootballTeam team = new FootballTeam(name, city, division);
                            lst_AFC.Items.Add(team);
                        }
                        else if (division == "NFC")
                        {
                            FootballTeam team = new FootballTeam(name, city, division);
                            lst_NFC.Items.Add(team);
                        }
                    }
                }
            }
        }