CapDemo.BL.QuestionBL.GetFileXML C# (CSharp) Méthode

GetFileXML() public méthode

public GetFileXML ( string file ) : List
file string
Résultat List
        public List<Question> GetFileXML(string file)
        {
            if (FA.FileContentXML(file) == null)
            {
                return null;
            }
            else
            {
                string content = FA.FileContentXML(file);
                int stt = 1;
                DataTable dtb = new DataTable();
                dtb.Columns.Add("Sequence", typeof(string));
                dtb.Columns.Add("QuestionTitle", typeof(string));
                dtb.Columns.Add("NameQuestion", typeof(string));
                dtb.Columns.Add("TypeQuestion", typeof(string));
                dtb.Columns.Add("AnswerContent", typeof(string));

                string[] QuestionContent = content.Split(new string[] { "</question>" }, StringSplitOptions.None);

                for (int i = 0; i < QuestionContent.Length - 1; i++)
                {
                    string[] QuestionItem = QuestionContent[i].Split(new string[] { "---" }, StringSplitOptions.None);
                    dtb.Rows.Add(stt.ToString(),QuestionItem[1], QuestionItem[2], QuestionItem[0], QuestionItem[3]);
                    stt++;
                }

                List<Question> QuestionList = new List<Question>();
                if (dtb != null && dtb.Rows.Count > 0)
                {
                    foreach (DataRow item in dtb.Rows)
                    {
                        Question Question = new Question();
                        Question.Sequence = Convert.ToInt32(item["Sequence"]);
                        Question.QuestionTitle = item["QuestionTitle"].ToString();
                        Question.NameQuestion = item["NameQuestion"].ToString();
                        Question.TypeQuestion = item["TypeQuestion"].ToString();

                        //string AnswerContent = item["AnswerContent"].ToString().Trim().Replace("''","'");
                        string AnswerContent = item["AnswerContent"].ToString().Trim();
                        AnswerContent = AnswerContent.Replace("+++", "---");
                        Question.AnswerContent = AnswerContent;
                        QuestionList.Add(Question);
                    }
                    return QuestionList;
                }
                else
                {
                    return null;
                }
            }
        }

Usage Example

        //LOAD FILE QUESTION
        private void btn_LoadFile_Click(object sender, EventArgs e)
        {
            QuestionBL questionBL = new QuestionBL();
            OpenFileDialog OpenFile = new OpenFileDialog();
            DialogResult Result = OpenFile.ShowDialog();
            if (Result == DialogResult.OK)
            {
                if (Path.GetExtension(OpenFile.FileName) == ".txt" || Path.GetExtension(OpenFile.FileName) == ".xml" || Path.GetExtension(OpenFile.FileName) == ".jqz")
                {
                    if (Path.GetExtension(OpenFile.FileName) == ".txt")
                    {
                        dataGridView1.Columns.Clear();
                        QuestionBL QuestionBL = new QuestionBL();

                        List<DO.Question> QuestionList;
                        QuestionList = QuestionBL.GetFileTXT(OpenFile.FileName);
                        txt_FilePath.Text = OpenFile.FileName;
                        if (QuestionList != null)
                        {
                            dataGridView1.DataSource = QuestionList;
                            Showcolumns();
                            CheckQuestion();
                            if (WrongQuestion > 0)
                            {
                                MessageBox.Show("Tập tin nhập vào có " + WrongQuestion + " câu hỏi không hợp lệ (câu hỏi bị bôi đỏ). "
                                +"Hệ thống không cho phép chọn các câu hỏi đó. "
                                +"Vui lòng kiểm tra lại nội dung, loại câu hỏi và Phải chọn đáp án cho câu hỏi.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                        else
                        {
                            dataGridView1.Columns.Clear();
                            txt_FilePath.Text = "";
                            MessageBox.Show("Tải file không thành công. Bạn định dạng file không hợp lý.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else
                    {
                        //xml
                        if (Path.GetExtension(OpenFile.FileName) == ".xml")
                        {
                            dataGridView1.Columns.Clear();
                            QuestionBL QuestionBL = new QuestionBL();

                            List<DO.Question> QuestionList;
                            QuestionList = QuestionBL.GetFileXML(OpenFile.FileName);
                            txt_FilePath.Text = OpenFile.FileName;
                            if (QuestionList != null)
                            {
                                dataGridView1.DataSource = QuestionList;
                                Showcolumns();
                                CheckQuestion();
                                if (WrongQuestion > 0)
                                {

                                    MessageBox.Show("Tập tin nhập vào có " + WrongQuestion + " câu hỏi không hợp lệ (câu hỏi bị bôi đỏ). "
                                + "Hệ thống không cho phép chọn các câu hỏi đó. "
                                + "Vui lòng kiểm tra lại nội dung, loại câu hỏi và Phải chọn đáp án cho câu hỏi.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                            else
                            {
                                dataGridView1.Columns.Clear();
                                txt_FilePath.Text = "";
                                MessageBox.Show("Tải file không thành công. Bạn định dạng file không hợp lý.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                        else
                        {
                            //hot potatoes
                            dataGridView1.Columns.Clear();
                            QuestionBL QuestionBL = new QuestionBL();

                            List<DO.Question> QuestionList;
                            QuestionList = QuestionBL.GetFileHotPotato(OpenFile.FileName);
                            txt_FilePath.Text = OpenFile.FileName;
                            if (QuestionList != null)
                            {
                                dataGridView1.DataSource = QuestionList;
                                Showcolumns();
                                CheckQuestion();
                                if (WrongQuestion > 0)
                                {

                                    MessageBox.Show("Tập tin nhập vào có " + WrongQuestion + " câu hỏi không hợp lệ (câu hỏi bị bôi đỏ). "
                                + "Hệ thống không cho phép chọn các câu hỏi đó. "
                                + "Vui lòng kiểm tra lại nội dung, loại câu hỏi và Phải chọn đáp án cho câu hỏi.", "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }
                            }
                            else
                            {
                                dataGridView1.Columns.Clear();
                                txt_FilePath.Text = "";
                                MessageBox.Show("Tải file không thành công. Bạn định dạng file không hợp lý.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }

                    }
                }
                else
                {
                    dataGridView1.Columns.Clear();
                    txt_FilePath.Text = "";
                    MessageBox.Show("Tải file không thành công. Bạn phải chọn File .XML hoặc .TXT.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                chk_CheckAll.Checked = false;
            }
        }