Exemple #1
0
        public void ReadXML(DataGridView dataGridView)
        {
            XmlNodeList xmlNodeList, xmlNodeList2;
            XmlNode     xmlNode3;

            System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
            xmlDocument.Load("textWriter.xml");
            xmlNodeList = xmlDocument.GetElementsByTagName("connection");
            connectionPropertiesList = new List <ConnectionProperties>();
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                ConnectionProperties connectionProperty = new ConnectionProperties();
                connectionProperty.ConnectionName   = (xmlNode["connectionName"].InnerText);
                connectionProperty.ModbusTCPAddress = (xmlNode["ipAddress"].InnerText);
                connectionProperty.Port             = Int32.Parse(xmlNode["port"].InnerText);
                connectionProperty.CyclicFlag       = bool.Parse(xmlNode["cyclicFlag"].InnerText);
                connectionProperty.CycleTime        = Int32.Parse(xmlNode["cycleTime"].InnerText);
                xmlNode3 = xmlNode["functionCodes"];
                while (xmlNode3 != null)
                {
                    xmlNodeList2 = xmlNode3.ChildNodes;
                    FunctionProperties functionProperty = new FunctionProperties();
                    foreach (XmlNode xmlNode2 in xmlNodeList2)
                    {
                        if (xmlNode2.Name == "functionCode")
                        {
                            switch (xmlNode2.InnerText)
                            {
                            case "ReadCoils":
                                functionProperty.FunctionCode = FunctionCode.ReadCoils;
                                break;

                            case "ReadDiscreteInputs":
                                functionProperty.FunctionCode = FunctionCode.ReadDiscreteInputs;
                                break;

                            case "ReadHoldingRegisters":
                                functionProperty.FunctionCode = FunctionCode.ReadHoldingRegisters;
                                break;

                            case "ReadInputRegisters":
                                functionProperty.FunctionCode = FunctionCode.ReadInputRegisters;
                                break;
                            }
                        }
                        if (xmlNode2.Name == "startingAddress")
                        {
                            functionProperty.StartingAdress = Int32.Parse(xmlNode2.InnerText);
                        }
                        if (xmlNode2.Name == "quantity")
                        {
                            functionProperty.Quantity = Int32.Parse(xmlNode2.InnerText);
                        }
                    }
                    connectionProperty.FunctionPropertiesList.Add(functionProperty);
                    xmlNode3 = xmlNode3.NextSibling;
                }
                connectionPropertiesList.Add(connectionProperty);
            }
            if (connectionPropertiesListChanged != null)
            {
                connectionPropertiesListChanged(this);
            }
            xmlNodeList = xmlDocument.GetElementsByTagName("dataGridViewLines");
            dataGridView.Rows.Clear();
            dataGridView.AllowUserToAddRows = false;
            foreach (XmlNode xmlNode in xmlNodeList)
            {
                dataGridView.Rows.Add();
                if (xmlNode["columnConnection"] != null)
                {
                    dataGridView[0, dataGridView.Rows.Count - 1].Value = xmlNode["columnConnection"].InnerText;
                }
                dataGridView.ClearSelection();
                dataGridView.CurrentCell = null;

                if (xmlNode["columnAddress"] != null)
                {
                    dataGridView[1, dataGridView.Rows.Count - 1].Value = xmlNode["columnAddress"].InnerText;
                }
                if (dataGridViewChanged != null)
                {
                    dataGridViewChanged(this);
                }
                if (xmlNode["columnTag"] != null)
                {
                    dataGridView[2, dataGridView.Rows.Count - 1].Value = xmlNode["columnTag"].InnerText;
                }
                if (xmlNode["columnDataType"] != null)
                {
                    dataGridView[3, dataGridView.Rows.Count - 1].Value = xmlNode["columnDataType"].InnerText;
                }
            }
            dataGridView.AllowUserToAddRows = true;
        }