XML节点访问与更新

1. XML节点访问与更新.

2. DataGridView, 把 string[] bind到DataGridView.

3. 通过封装器, 做成List<StringValue>.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace ChangeMultipleChoiceRandomization
{
    public partial class ThorXMLEditor : Form
    {
        public ThorXMLEditor()
        {
            InitializeComponent();
            fillLanguagesToCB();
            disableSetButtons();
        }

        void fillLanguagesToCB()
        {
            cb_Lan.DropDownStyle = ComboBoxStyle.DropDownList;
            string[] languages = new string[]{
                "CHS",
                "CHT",
                "DEU",
                "ESN",
                "ESP",
                "FRA",
                "ITA",
                "JPN",
                "KOR",
                "PLK",
                "PTB",
                "PTG",
                "RUS",
                "TRK"
            };
            cb_Lan.Items.AddRange(languages);
        }

        void disableSetButtons()
        {
            bt_SetFG.Enabled = false;
            bt_SetTitle.Enabled = false;
        }

        void enableSetButtons()
        {
            bt_SetFG.Enabled = true;
            bt_SetTitle.Enabled = true;
        }

        void clearControls()
        {
            tb_Title.Text = "Exam Title";
            gv_FG.DataSource = null;
            lblFG.Text = "NONE";
            lblTitle.Text = "NONE";
        }

        private void bt_UpdateLan_Click(object sender, EventArgs e)
        {
            int resultCount = 0;
            string newlanValue = string.Empty;
            string thorXML = tb_ThorPath.Text.Trim();
            if (!File.Exists(thorXML))
                return;
            if (cb_Lan.Text.Trim() != "")
            {
                newlanValue = cb_Lan.Text.Trim();
            }

            if (newlanValue != string.Empty)
            {
                ProccedThorXML clsThor = new ProccedThorXML(thorXML);
                clsThor.SetLanguage(newlanValue, out resultCount);
                clsThor.SaveBack();
            }

            lblLan.Text = "Changed language in " + resultCount + " places!";
        }

        private void bt_GetFG_Click(object sender, EventArgs e)
        {
            clearControls();
            string newlanValue = string.Empty;
            string thorXML = tb_ThorPath.Text.Trim();
            if (!File.Exists(thorXML))
                return;

            ProccedThorXML clsThor = new ProccedThorXML(thorXML);
            string[] FGsString = clsThor.GetFunctionalGroup();

            //Exam Title
            tb_Title.Text = clsThor.GetExamTitle();

            if (FGsString != null && FGsString.Length > 0)
            {
                List<StringValue> bindableValue = new List<StringValue>();

                foreach (string item in FGsString)
                {
                    bindableValue.Add(new StringValue(item));
                }
                gv_FG.DataSource = bindableValue;
            }
            enableSetButtons();
        }

        private void bt_SetFG_Click(object sender, EventArgs e)
        {           
            int resultCount = 0;
            string newlanValue = string.Empty;
            string thorXML = tb_ThorPath.Text.Trim();
            if (!File.Exists(thorXML))
                return;
            string[] newFGs = null;
            if (gv_FG.Rows.Count > 0)
            {
                newFGs = new string[gv_FG.Rows.Count];
                for (int i = 0; i < gv_FG.Rows.Count; i++)
                {
                    newFGs[i] = gv_FG.Rows[i].Cells[0].Value.ToString();
                }
            }

            if (newFGs != null)
            {
                ProccedThorXML clsThor = new ProccedThorXML(thorXML);
                clsThor.SetFuntionalGroup(newFGs, out resultCount);
                clsThor.SaveBack();
            }

            lblFG.Text = "Changed " + resultCount + " FGs!";
        }

        private void bt_SetTitle_Click(object sender, EventArgs e)
        {
            int resultCount = 0;
            string newlanValue = string.Empty;
            string thorXML = tb_ThorPath.Text.Trim();
            if (!File.Exists(thorXML))
                return;

            string newTitle = tb_Title.Text.Trim();
            ProccedThorXML clsThor = new ProccedThorXML(thorXML);
            clsThor.SetExamTitle(newTitle, out resultCount);
            clsThor.SaveBack();

            lblTitle.Text = "Changed Exam Title in " + resultCount + " places!";
        }
    }

    public class ProccedThorXML
    {
        string _xmlPath = string.Empty;
        XmlDocument _xDoc = null;
        //public string XMLPath
        
//{
        
//    get
        
//    {
        
//        return _xmlPath;
        
//    }

        
//    set
        
//    {
        
//        _xmlPath = value;
        
//    }
        
//}
        public ProccedThorXML()
        {
        }

        public ProccedThorXML(string xmlPath)
        {
            _xmlPath = xmlPath;
            _xDoc = new XmlDocument();
            if (File.Exists(_xmlPath))
            {
                try
                {
                    _xDoc.Load(_xmlPath);
                }
                catch (Exception ex)
                { }
            }
        }

        public void SetLanguage(string language, out int resultCount)
        {
            resultCount = 0;
            if (_xDoc != null)
            {
                //Set language in ExmInfo
                string xPathExamInfo = string.Format(@"//ThorProject/ExamInfo/ExamLanguage");
                XmlNode examLanguageNode = _xDoc.SelectSingleNode(xPathExamInfo);
                if (examLanguageNode != null)
                {
                    examLanguageNode.InnerText = language;
                    resultCount++;
                }

                //Set language for item item by item
                string xPathItem = string.Format(@"//ThorProject/ExamItems/ExamItem");
                XmlNodeList itemList = _xDoc.SelectNodes(xPathItem);
                if (itemList.Count > 0)
                {
                    foreach (XmlNode node in itemList)
                    {
                        XmlNode lanNode = node.SelectSingleNode("ItemLanguage");
                        if (lanNode != null)
                        {
                            lanNode.InnerText = language;
                            resultCount++;
                        }
                    }
                }
            }
        }

        public string[] GetFunctionalGroup()
        {
            string[] result = null;

            string xPathFGs = string.Format(@"//ThorProject/FunctionalGroups/FunctionalGroup");
            XmlNodeList fgNodes = _xDoc.SelectNodes(xPathFGs);

            string xPathSubExam = string.Format(@"//ThorProject/SubExams/SubExam");
            XmlNodeList subexamNodes = _xDoc.SelectNodes(xPathSubExam);
            if (fgNodes != null && fgNodes.Count > 0)
            {
                result = new string[fgNodes.Count];
                for (int i = 0; i < fgNodes.Count; i++)
                {
                    result[i] = fgNodes[i].ChildNodes[0].InnerText.ToString();
                }
            }
            else if (subexamNodes != null && subexamNodes.Count > 0)
            {
                result = new string[subexamNodes.Count];
                for (int i = 0; i < subexamNodes.Count; i++)
                {
                    result[i] = subexamNodes[i].SelectSingleNode("SubExamTitle").InnerText.ToString();
                }
            }
            else { result = null; }

            return result;
        }

        public void SetFuntionalGroup(string[] newFGs, out int resultCount)
        {
            resultCount = 0;
            string xPathFGs = string.Format(@"//ThorProject/FunctionalGroups/FunctionalGroup");
            XmlNodeList fgNodes = _xDoc.SelectNodes(xPathFGs);

            string xPathSubExam = string.Format(@"//ThorProject/SubExams/SubExam");
            XmlNodeList subexamNodes = _xDoc.SelectNodes(xPathSubExam);
            if (fgNodes != null)
            {
                if (fgNodes.Count > 0 && (fgNodes.Count == newFGs.Length))
                {
                    for (int i = 0; i < fgNodes.Count; i++)
                    {
                        fgNodes[i].ChildNodes[0].InnerText = newFGs[i];
                        resultCount++;
                    }
                }
            }
            if (subexamNodes != null)
            {
                if (subexamNodes.Count > 0 && (subexamNodes.Count == newFGs.Length))
                {
                    for (int i = 0; i < subexamNodes.Count; i++)
                    {
                        subexamNodes[i].SelectSingleNode("SubExamTitle").InnerText = newFGs[i];
                        resultCount++;
                    }
                }
            }          

        }

        public string GetExamTitle()
        {
            string examTitle = string.Empty;
            string xPathExamTitle = string.Format(@"//ThorProject/ExamInfo/ExamTitle");
            XmlNode titleNode = _xDoc.SelectSingleNode(xPathExamTitle);
            if (titleNode != null)
            {
                examTitle = titleNode.InnerText.ToString();
            }

            return examTitle;
        }

        public void SetExamTitle(string title, out int resultCount)
        {
            resultCount = 0;
            string examTitle = string.Empty;
            string xPathExamTitle = string.Format(@"//ThorProject/ExamInfo/ExamTitle");
            XmlNode titleNode = _xDoc.SelectSingleNode(xPathExamTitle);
            if (titleNode != null)
            {
                titleNode.InnerText = title;
                resultCount++;
            }
        }

        public void SaveBack()
        {
            File.SetAttributes(_xmlPath, FileAttributes.Normal);
            _xDoc.Save(_xmlPath);
        }
    }

    //Just a wrapper for string
    /*Thats because DataGridView looks for properties of containing objects. For string there is just one property - length. So, you need a wrapper for a string like this*/
    public class StringValue
    {
        string _value;
        public string Value { get { return _value; } set { _value = value; } }

        public StringValue(string str)
        {
            _value = str;
        }
    }
}
原文地址:https://www.cnblogs.com/qixue/p/2796518.html