C#关于xml文件和TreeView之间的转换解析

  public partial class ParseXmlFilesForm : Form
    {
        public static WirelessCharge celfras = new WirelessCharge();
        public static StreamWriter logSW = null;
        public static FileStream logFS = null;
        #region 变量声明
        private string xmlFilePath;
        private string rootName;
        DataTable dt = new DataTable();
        private List<NodeMes> nodeList = new List<NodeMes>();
        private Boolean bState;
        private string appendText;
        #endregion
        public ParseXmlFilesForm()
        {
            InitializeComponent();
        }

        private void ParseXmlFilesForm_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("InputName");
            dt.Columns.Add("InputValue");
            dt.Columns.Add("OutputName");
            dt.Columns.Add("OutputValue");
            dataGridView1.DataSource = dt;
            dataGridView1.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellValueChanged);

            btnSave.Enabled = false;
            btnOpenFile.Height = 25;
            btnOpenFile.Width = 60;
            btnSave.Height = 25;
            btnSave.Width = 60;
            btnRun.Height = 25;
            btnRun.Width = 60;
            btnClear.Height = 25;
            btnClear.Width = 60;

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        }
     //析构函数
        ~ParseXmlFilesForm() { }

        #region 将xml文件转换为TreeNode
        public void Xml2Tree()
        {
            //string xmlFilePath = "E:\test.xml";
            XmlDocument xmlDoc = GetXmlDocByFilePath(xmlFilePath);

            //xmlDoc.Load(this.xmlFilePath);
            //获取根节点的名字
            rootName = xmlDoc.DocumentElement.Name;
            //获取根节点
            XmlNode root = xmlDoc.SelectSingleNode(rootName);
            //把根节点添加到TreeView
            TreeNode rootTreeNode = new TreeNode();
            rootTreeNode.Text = rootName;
            treeView1.Nodes.Add(rootTreeNode);

            TransXml(root.ChildNodes, rootTreeNode, "");
            //ShowNodeMes();
            //MessageBox.Show(treeView1.Nodes[0].Nodes[4].Nodes[9].Nodes[2].Nodes.Count + "");

        }
        /// <summary>
        /// 显示listNode中所有testNode的信息
        /// </summary>
        private void ShowNodeMes()
        {

            byte I2CAddress = Convert.ToByte(nodeList[0].NodeList[0].ChildNodes[3].InnerText.ToString(), 16);
            richTextBox1.AppendText(I2CAddress.ToString());

            try
            {
                foreach (NodeMes n in nodeList)
                {
                    richTextBox1.AppendText(n.NodeName + "\t");

                    foreach (XmlAttribute attr in n.AttrColl)
                    {
                        richTextBox1.AppendText(attr.Name + " = " + attr.Value + "\t");
                    }
                    richTextBox1.AppendText("\n**************" + n.NodeList[0].Name + "*****************\n");
                    foreach (XmlNode node in n.NodeList[0].ChildNodes)
                    {
                        richTextBox1.AppendText("\n" + node.Name + " = " + node.InnerText);
                    }
                    richTextBox1.AppendText("\n**************" + n.NodeList[1].Name + "*****************\n");
                    foreach (XmlNode node in n.NodeList[1].ChildNodes)
                    {
                        richTextBox1.AppendText("\n" + node.Name + " = " + node.InnerText);
                    }
                    richTextBox1.AppendText("\n----------------------------------------------------------\n");
                }
            }
            catch (Exception ex) { }

        }
        /// <summary>
        /// 将xml中node的Attribute值添加到TreeView中
        /// </summary>
        /// <param name="subXmlNode"></param>
        /// <param name="node"></param>
        private void AddAttr2Tree(XmlNode subXmlNode, TreeNode node)
        {
            TreeNode attrNode = new TreeNode();
            foreach (XmlAttribute attr in subXmlNode.Attributes)
            {
                attrNode.Name = attr.Name;
                attrNode.Text = attrNode.Name + " = " + attr.Value;
                node.Nodes.Add(attrNode);
                node.Tag = "isAttribute";
            }
        }

        private void TransXml(XmlNodeList childNodes, TreeNode node, string order)
        {
            for (int i = 0; i < childNodes.Count; i++)
            {
                XmlNode xmlNode = childNodes[i];
                TreeNode subTreeNode = new TreeNode();

                AddNode2List(xmlNode);

                #region 添加Attribute节点
                try
                {
                    if (xmlNode.Attributes.Count > 0 && xmlNode.Attributes != null)
                    {
                        AddAttr2Tree(xmlNode, subTreeNode);
                    }
                }
                catch (Exception ex)
                {

                }
                #endregion

                #region 添加非Attribute 节点
                if (xmlNode.HasChildNodes)
                {
                    subTreeNode.Text = "[" + order + i.ToString("X") + "]" + xmlNode.Name;
                    subTreeNode.Name = xmlNode.Name;
                    subTreeNode.Tag = "isTreeNode";
                    TransXml(xmlNode.ChildNodes, subTreeNode, order + i.ToString("X"));
                }
                else
                {
                    if (xmlNode.Value != null)
                    {
                        subTreeNode.Text = xmlNode.InnerText;
                        subTreeNode.Name = xmlNode.Name;
                        subTreeNode.Tag = "isTreeNode";
                    }
                    else
                    {
                        subTreeNode.Text = xmlNode.Name;
                        subTreeNode.Name = xmlNode.Name;
                        subTreeNode.Tag = "NoValue";
                    }

                }
                node.Nodes.Add(subTreeNode);
                //node.Tag = "isTreeNode";
                #endregion

                #region 判断节点是不是包含input and output 的testNode
                if (subTreeNode.Nodes.Count >= 3)
                {
                    string[] strInput = subTreeNode.Nodes[1].Text.Split(‘]‘);
                    string[] strOutput = subTreeNode.Nodes[2].Text.Split(‘]‘);
                    if (strInput.Length == 2 && strOutput.Length == 2)
                    {
                        string inputName = strInput[1];
                        string outputName = strOutput[1];
                        if ("INPUT".Equals(inputName) && "OUTPUT".Equals(outputName))
                        {
                            //richTextBox1.AppendText(input + "\t" + output + "\n");
                            subTreeNode.Tag = "testNode";
                        }
                    }
                }
                #endregion
            }
        }
        /// <summary>
        /// 将testNode保存到nodeList中
        /// </summary>
        /// <param name="xmlNode"></param>
        public void AddNode2List(XmlNode xmlNode)
        {
            NodeMes nodeMes = new NodeMes(xmlNode.Name, xmlNode.InnerText, xmlNode.ChildNodes, xmlNode.Attributes);
            if (nodeMes.NodeList.Count >= 2 && "INPUT".Equals(nodeMes.NodeList[0].Name) && "OUTPUT".Equals(nodeMes.NodeList[1].Name))
            {
                nodeList.Add(nodeMes);
            }
        }

        public XmlDocument GetXmlDocByFilePath(string xmlFilePath)
        {
            if (string.IsNullOrEmpty(xmlFilePath) || !File.Exists(xmlFilePath))
            {
                return null;
            }
            XmlDocument xmlDoc = new XmlDocument();
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;
            try
            {
                XmlReader reader = XmlReader.Create(@xmlFilePath, settings);
                xmlDoc.Load(reader);
                reader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("请检查Xml文件的格式是否正确,文件存放路径为:" + xmlFilePath + "\n错误信息提示:" + ex.ToString(), "提示");
            }
            return xmlDoc;
        }
        #endregion

        #region click event
         private void btnOpenFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "XML文件|*.xml";
            if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }
            else
            {
                xmlFilePath = System.IO.Path.GetFullPath(openFileDialog1.FileName);
                foreach (string str in comboBox1.Items)
                {
                    if (xmlFilePath.CompareTo(str) == 0) {
                        MessageBox.Show("你已经打开该xml文件!","提示");
                        return;
                    }
                }
                comboBox1.Text = xmlFilePath;
                comboBox1.Items.Add(xmlFilePath);

                Xml2Tree();
                btnSave.Enabled = true;
            }

        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            int nodeCount = treeView1.Nodes.Count;
            for (int i = 0; i < nodeCount; i++)
            {
                treeView1.Nodes[0].Remove();
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            dt.Clear();
            TreeNode node = treeView1.SelectedNode;

            if ("testNode".Equals(node.Tag))
            {
                //将选择的node的信息显示到DataGridView中
                AddNode2GridView(node);
            }
        }
        #endregion

        #region DataGridView的处理
        private void AddNode2GridView(TreeNode node)
        {
            int inCount = node.Nodes[1].Nodes.Count;
            int outCount = node.Nodes[2].Nodes.Count;
            if (inCount > 0 && outCount > 0)
            {
                int max = inCount >= outCount ? inCount : outCount;
                if (max == inCount)
                {
                    for (int i = 0; i < max; i++)
                    {

                        if (i > outCount - 1)
                        {
                            DataRow dr = dt.NewRow();
                            dr["OutputName"] = "";
                            dr["OutputValue"] = "";
                            dr["InputName"] = node.Nodes[1].Nodes[i].Text;
                            dr["InputValue"] = node.Nodes[1].Nodes[i].Nodes[0].Text;
                            dt.Rows.Add(dr);
                        }
                        else
                        {
                            DataRow dr = dt.NewRow();
                            dr["OutputName"] = node.Nodes[2].Nodes[i].Text;
                            try {
                                dr["OutputValue"] = node.Nodes[2].Nodes[i].Nodes[0].Text;
                            } catch (Exception ex) {
                                dr["OutputValue"] = "";
                            }

                            dr["InputName"] = node.Nodes[1].Nodes[i].Text;
                            dr["InputValue"] = node.Nodes[1].Nodes[i].Nodes[0].Text;
                            dt.Rows.Add(dr);
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < outCount; i++)
                    {
                        if (i > inCount - 1)
                        {
                            DataRow dr = dt.NewRow();
                            dr["OutputName"] = node.Nodes[2].Nodes[i].Text;
                            dr["OutputValue"] = node.Nodes[2].Nodes[i].Nodes[0].Text;
                            dr["InputName"] = "";
                            dr["InputValue"] = "";
                            dt.Rows.Add(dr);
                        }
                        else
                        {
                            DataRow dr = dt.NewRow();
                            dr["OutputName"] = node.Nodes[2].Nodes[i].Text;
                            dr["OutputValue"] = node.Nodes[2].Nodes[i].Nodes[0].Text;
                            dr["InputName"] = node.Nodes[1].Nodes[i].Text;
                            dr["InputValue"] = node.Nodes[1].Nodes[i].Nodes[0].Text;
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
        }

        //dataGridView中单元格内容修改的事件处理
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex % 2 != 0)
            {
                string cellValue = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                string cellName = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex - 1].Value.ToString();

                CircleNode(cellName, cellValue, treeView1.Nodes);
            }
        }

        //循环遍历所有节点,找到对应DataGridView修改的节点
        public void CircleNode(string nodeName, string nodeText, TreeNodeCollection nodeColl)
        {
            foreach (TreeNode node in nodeColl)
            {
                if (nodeName.Equals(node.Text))
                {
                    node.Nodes[0].Text = nodeText;
                    return;
                }
                if (node.Nodes.Count > 0)
                {
                    CircleNode(nodeName, nodeText, node.Nodes);
                }
            }
        }
        #endregion

        #region 保存处理
        private void btnSave_Click(object sender, EventArgs e) {
            try {
                SaveTreeView2XML();
                MessageBox.Show("保存成功!");
            }
            catch (Exception ex) {
                MessageBox.Show("保存失败!");
            }

        }

        private void SaveTreeView2XML()
        {
            XDeclaration dec = new XDeclaration("1.0", "utf-8", "yes");
            XDocument doc = new XDocument(dec);
            XElement root = new XElement(rootName);

            foreach (TreeNode node in treeView1.Nodes[0].Nodes)
            {
                XElement e = CreateElements(node, root);
                root.Add(e);
            }
            doc.Add(root);
            doc.Save("TreeView2XML.xml");
        }

        private XElement CreateElements(TreeNode node, XElement elementRoot)
        {
            XElement root = CreateElement(node);
            if (node.Nodes.Count > 0)
            {
                foreach (TreeNode n in node.Nodes)
                {
                    XElement e = CreateElements(n, root);
                    if (n.Tag != null)
                    {
                        if ("isAttribute".Equals(n.Tag.ToString()))
                        {
                            string[] str = n.Text.ToString().Replace(" ", "").Split(‘=‘);
                            root.SetAttributeValue(str[0], str[1]);
                        }
                        else if (n.Nodes.Count > 0)
                            root.Add(e);
                        else if (n.Tag.Equals("NoValue"))
                        {
                            root.Add(e);
                            //root.Add("<" + n.Name + ">" + "</" + n.Name + ">");
                        }
                        else
                            root.Add(n.Text.ToString());
                    }

                }
            }
            return root;
        }

        private XElement CreateElement(TreeNode node)
        {
            XElement element = null;
            try
            {
                if (node.Nodes.Count > 0)
                {
                    element = new XElement(node.Name);
                }
            }
            catch (Exception ex)
            {
                richTextBox1.AppendText(ex.Message);
            }
            return element;
        }
        #endregion
        public bool CFS_CSV_Init(ref string sLogName)
        {
            try
            {
                //code
                string logName = System.Environment.CurrentDirectory + "\\celfras" + DateTime.Now.ToString("-MM-dd-HH-mm-ss") + ".csv";
                sLogName = logName;
                logFS = new FileStream(logName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write);
                logSW = new StreamWriter(logFS);
                logSW.WriteLine("test item,test value,results");
                logSW.WriteLine("BGR test,0x21,22.34");
                logSW.WriteLine("OTP test,0x11,21.34");
                logSW.Flush();
                return true;
            }
            catch (Exception ex)
            {
                logSW.WriteLine("[ERR]" + ex.Message);
                logSW.Flush();
                return false;
            }
        }

         static void TestDUTCWQ1100()
        {
            string sLogName = null;
            celfras.CFS_LOG_Init(ref sLogName);
            celfras.CFS_CWQ1100_I2CInit(0x24, 0.15);
            celfras.CFS_CWQ1000_SetGPIO2Low();
            //int iData = 0;
            //celfras.CFS_DUT_ReadRegister(0x5c,ref iData);
            celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x41);
            // celfras.CFS_DUT_ReadRegister(0x5c, ref iData);
            celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x42);
            // celfras.CFS_DUT_ReadRegister(0x5c, ref iData);
            celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x43);
            celfras.CFS_Term();
        }
        static void TestPWR()
        {
            celfras.CFS_PWR_Init("GPIB0::5::INSTR"); // connect error
            celfras.CFS_PWR_OnOff(true);
            celfras.CFS_PWR_SetVoltage(7.0);
            celfras.CFS_PWR_SetCurrent(1.0);
            double dVoltage = 0.0;
            celfras.CFS_PWR_GetVoltage(ref dVoltage);
            double dCurrent = 0.0;
            celfras.CFS_PWR_GetCurrent(ref dCurrent);
        }

        private void btnRun_Click(object sender, EventArgs e) {
            appendText = "初始化DUT, OSC, PWD, 关闭电子负载,VRECT = 5.5V VPP = 3V \n";
            richTextBox1.Text = appendText;
            string logName = null;
            celfras.CFS_LOG_Init(ref logName);

            InvokeDll();
        }

        public void InvokeDll()
        {
            try
            {
                foreach (NodeMes node in nodeList)
                {
                    //XmlNodeList list = node.NodeList;
                    switch (node.NodeName)
                    {
                        case "Init_DUT":
                            RunInitDut(node);
                            break;
                        case "Init_Tester_PWR":
                            RunInitTesterPWR(node);
                            break;
                        case "Init_CFS_OSC":
                            Run_Init_CFS_OSC(node);
                            break;
                        case "BGRVoltageCalibration":
                            Run_BGR_Voltage_Calibration(node);
                            break;
                        case "OSCFrequentyCalibration":
                            Run_OSC_Frequenty_Calibration(node);
                            break;
                        case "VOUT_Calibration":
                            Run_VOUT_Calibration(node);
                            break;
                        case "TRIM_ADC_IOUT_Calibration":
                            Run_TRIM_ADC_IOUT_Calibration(node);
                            break;
                        case "OTP_Write":
                            Run_OTP_Write(node);
                            break;
                        case "BGR_Voltage_Test":
                            Run_BGR_Voltage_Test();
                            break;
                        case "OSC_Frequency_Test":
                            Run_OSC_Frequency_Test();
                            break;
                        case "LDO_Voltage_Test":
                            break;
                        case "ADC_Test":
                            break;
                        case "Current_Sensor_Test":
                            break;
                        case "ENB_Test":
                            break;
                        case "OVP_Test":
                            break;
                        case "OCL_Test":
                            break;
                        case "Test_Port_Test":
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示!");
            }
        }

        #region device operation
        public void RunInitDut(NodeMes node)
        {
            ShowRichTextBox("Init Dut",true);
            byte I2CAddress = Convert.ToByte(node.NodeList[0].ChildNodes[3].InnerText, 16);
            double I2CClock = double.Parse(node.NodeList[0].ChildNodes[4].InnerText);
            celfras.CFS_CWQ1100_I2CInit(I2CAddress, I2CClock);
            celfras.CFS_CWQ1000_SetGPIO2Low();
            celfras.CFS_CWQ1100_I2CInit();
            celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x41);
            celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x42);
            celfras.CFS_CWQ1100_WriteRegister(0x5c, 0x43);
            MessageBox.Show("Init_DUT success!", "提示!");
        }

        private void RunInitTesterPWR(NodeMes node)
        {
            ShowRichTextBox("Init Tester PWR",true);
            celfras.CFS_PWR_Init(node.NodeList[0].ChildNodes[1].InnerText); // connect error
            celfras.CFS_PWR_SetVoltage(5.5);
            celfras.CFS_PWR_SetCurrent(2.0);
            celfras.CFS_PWR_OnOff(true);
            richTextBox1.Text += "\n Init Tester PWR success!";
        }

        private void Run_Init_CFS_OSC(NodeMes node)
        {
            ShowRichTextBox("Init CFS OSC",true);
            celfras.CFS_OSC_Init(node.NodeList[0].ChildNodes[0].InnerText);
        }

        #region CWQ1100 TRIM_CFG OTP Write Test
        private void Run_BGR_Voltage_Calibration(NodeMes node)
        {
            ShowRichTextBox("BGR Voltage Calibration",true);
            if ("True".Equals(node.AttrColl[0].Value.ToString()))
            {
                int iReg = 0;
                bState = celfras.CFS_TM_BGRVoltageCalibration(1, ref iReg);
                if (bState == true)
                {
                    richTextBox1.Text += "\n iReg = " + iReg;
                }
                else
                    richTextBox1.Text += "\n Cannot find Value";
            }
        }

        private void Run_OSC_Frequenty_Calibration(NodeMes node)
        {
            ShowRichTextBox("OSC Frequenty Calibration",true);
            if ("True".Equals(node.AttrColl[0].Value.ToString()))
            {
                int iReg2 = 0;
                bState = celfras.CFS_OSC_FreqCal_SendCmd();
                if (bState == false)
                    throw new Exception("CFS_OSC_FreqCal_SendCmd()");
                bState = celfras.CFS_TM_OSCFrequencyCalibration(2, ref iReg2);
                if (bState == true)
                {
                    richTextBox1.Text += "\n iReg = " + iReg2 ;
                }
                else
                    richTextBox1.Text += "\n 测试失败error";
            }
        }

        private void Run_VOUT_Calibration(NodeMes node)
        {
            ShowRichTextBox("VOUT Calibration",true);
            richTextBox1.Text += "\n 打开电子负载!";
            if ("True".Equals(node.AttrColl[0].Value.ToString()))
            {
                //需要接上电子负载,设置1A
                int iReg29 = int.Parse(node.NodeList[0].ChildNodes[0].InnerText);
                int iReg2A = int.Parse(node.NodeList[0].ChildNodes[1].InnerText);
                //celfras.CFS_OSC_Init("USB0::0x2A8D::0x1766::MY57251817::0::INSTR");
                //CFS_TM_VOUTVoltageCalibration
                celfras.CFS_OSC_VoutCal_SendCmd();
                bState = celfras.CFS_TM_VOUTVoltageCalibration(3, ref iReg29, ref iReg2A);
                if (bState == true)
                {
                    richTextBox1.AppendText("iReg29 = " + iReg29 + "\t" + "iReg2A = " + iReg2A + "\n");
                }
                else
                    richTextBox1.AppendText("测试失败error \n");
            }
        }

        private void Run_TRIM_ADC_IOUT_Calibration(NodeMes node)
        {
            ShowRichTextBox(" TRIM ADC IOUT Calibration ",true);
            if ("True".Equals(node.AttrColl[0].Value.ToString()))
            {
                richTextBox1.AppendText("先调整电源为5.45V,使得示波器第4个通道测试值为5.4V\n");
                celfras.CFS_TM_ChangeVRECT(4);
                int iReg28 = 0;
                //celfras.CFS_OSC_Init("USB0::0x2A8D::0x1766::MY57251817::0::INSTR");
                celfras.CFS_OSC_ADCIOUTCal_SendCmd();
                bState = celfras.CFS_TM_ADCIOUTCalibration(1, ref iReg28);
                if (bState == true)
                {
                    richTextBox1.AppendText("iReg28 = " + iReg28 + "\n");
                }
            }
        }

        private void Run_OTP_Write(NodeMes node)
        {
            ShowRichTextBox(" OTP Write ",true);
            if ("True".Equals(node.AttrColl[0].Value.ToString()))
            {
                richTextBox1.AppendText("先调整VPP电源为8V,进行写OTP操作\n");
                celfras.CFS_TM_OTPWrite();

                richTextBox1.AppendText("测试结束写log\n");
                celfras.CFS_TM_RUNSummary();
                celfras.CFS_Term();
            }
        }
        #endregion

        #region function test
        private void Run_BGR_Voltage_Test()
        {
            ShowRichTextBox("BGR Voltage Test",true);
            celfras.CFS_BGR_VoltageTest(true);
            celfras.CFS_OSC_SendCmd();
            double dVoltage = 0.0;
            celfras.CFS_OSC_GetVoltage(1, ref dVoltage);
            richTextBox1.AppendText("ANA_TEST = " + dVoltage + "\n");
            celfras.CFS_BGR_VoltageTest(false);
        }

        private void Run_OSC_Frequency_Test()
        {
            ShowRichTextBox("OSC Frequency Test",true);
            celfras.CFS_OSC_FrequencyTest(true);
            double dFrequency = 0.0;
            celfras.CFS_OSC_GetFrequency(2, ref dFrequency);
            richTextBox1.AppendText("DIG_TEST = " + dFrequency + "\n");
            celfras.CFS_OSC_FrequencyTest(false);

        }

        #endregion

        public void ShowRichTextBox(string str,Boolean IsMethodName)
        {
            if (IsMethodName)
            {
                appendText += "================== " + str + " ==================\n";
            }
            else {
                appendText += str + "\n";
            }
            richTextBox1.Text = appendText;
        }

        #endregion

        public class NodeMes
        {
            public string NodeName { get; set; }
            public string NodeText { get; set; }
            public XmlNodeList NodeList { get; set; }
            public XmlAttributeCollection AttrColl { get; set; }

            public NodeMes(string name, string text, XmlNodeList node, XmlAttributeCollection attrColl)
            {
                NodeName = name;
                NodeText = text;
                NodeList = node;
                AttrColl = attrColl;
            }
        }

    }
时间: 2024-10-12 11:48:59

C#关于xml文件和TreeView之间的转换解析的相关文章

通过XML文件实现人物之间的对话

一.建立一个XML文档,放在项目中Assert/Resources/XML文件下 XML的内容如下: 1 <?xml version="1.0" encoding="utf-8"?> 2 3 <dialogues1> 4 <dialogue> 5 <role>门派长老</role> 6 <detail>李清,你找我意欲何事?</detail> 7 </dialogue>

Android程序解析XML文件的方法及使用PULL解析XML案例

一.一般解析XML文件的方法有SAX和DOM.PULL (1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准.DOM是以层次结构组织的节点或信息片断的集合.这个层次结构允许开发人员在树中寻找特定信息.分析该结构通常需要加载整个文档和构造层次结构,然后才能做任何工作.由于它是基于信息层次的,因而DOM被认为是基于树或基于对象的.DOM 以及广义的基于树的处理具有几个优点.首先,由于树在内存中是持久的,因此可以修改它以便应用程序能对数据和结构

图片文件和Bitmap之间的转换

图片文件转为Bitmap对象String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath); 如果图片过大,可能导致Bitmap对象装不下图片解决办法:String filePath="c:/01.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(filePath,getBitmapOption(2)); //将图片的长和宽缩小味原来的

文件与字符串之间的转换

/// <summary> /// 将传进来的文件转换成字符串 /// </summary> /// <param name="FilePath">待处理的文件路径(本地或服务器)</param> /// <returns></returns> public string FileToBinary(string FilePath) { FileStream fs = new FileStream(FilePath,

java基础之概谈xml文件解析

XML已经成为一种很通用的数据交换格式,它的平台无关性,语言无关性,系统无关性,给数据集成与交互带来了极大的方便.诸多web应用框架,其可配置的编程方式,给我们的开发带来了很大程度的便捷,但细细想来,它们的应用无一不是java bean与xml之间的转换解析.本文将对xml的两种操作标准DOM和SAX,从它们各自的特点.适用范围等方面进行简单介绍. DOM (Document Object Model) :DOM标准中,采用W3C标准表示XML,有多重语言支持,因此其跨平台性很好.采用DOM规范

PowerShell技巧:使用XPath语法查询XML文件

[TechTarget中国原创] XML是存储结构化数据的一个很好的途径,但是想要让数据在其中发挥作用又会有些困难.每一种语言都有其特定方式来查询XML文件中的命名空间.元素及属性.PowerShell也不例外.PowerShell在查询XML文件时会略有不同,而该技巧主要依赖于Select-Xml命令和XPath语法上. XPath是定义XML文件组成的语言.XPath早在1999年就已经存在,一直被用于查询XML文件最标准的方法. XPath将XML文件定义为树.XML文件中的每个节点都节点

使用Pull解析器生成XML文件

有些时候,我们需要生成一个XML文件,生成XML文件的方法有很多,如:可以只使用一个StringBuilder组拼XML内容,然后把内容写入到文件中:或者使用DOM API生成XML文件,或者也可以使用pull解析器生成XML文件,这里推荐大家使用Pull解析器. 1.使用Pull解析器生成一个与itcast.xml文件内容相同的myitcast.xml文件,代码在下方 public static String writeXML(List<Person> persons, Writer wri

測试之路2——对照XML文件1

才来几天,老大又给了我一个新的任务.不像曾经的建100个任务project那么坑爹,却还是顿时让我感觉压力山大. 由于在这之前,我改了他写的例程,用于生成新的任务项目,事实上任务项目就是通过XML文件进行參数传递,底层早已经封装好了.可是问题出来了,你新建任务须要传过去一个XML文件,可是server端生成任务还会返回一个XML文件,而我对于server端并不了解,我仅仅知道server生成了一个XML文件,可是它生成的XML中的參数是否和我传过去的參数同样? 所以老大要我写一个方法,去比对这两

测试之路2——对比XML文件1

才来几天,老大又给了我一个新的任务.不像以前的建100个任务工程那么坑爹,却还是顿时让我感觉压力山大. 因为在这之前,我改了他写的例程,用于生成新的任务项目,其实任务项目就是通过XML文件进行参数传递,底层早已经封装好了.但是问题出来了,你新建任务需要传过去一个XML文件,但是服务器端生成任务还会返回一个XML文件,而我对于服务器端并不了解,我只知道服务器生成了一个XML文件,但是它生成的XML中的参数是否和我传过去的参数相同? 所以老大要我写一个方法,去比对这两份XML文件.(自己传到服务器和