简体   繁体   中英

How to repopulate datagridview with XML file

I've tried searching for this a lot, but I'm having no luck unfortunately. I have an application which allows you to select a COM port and its Baud rate, and then connect it to another COM port (so a signal comes into one port and out of the other). I have a save function which makes it save into an XML format.

I want to be able to open that save file so that all of the information from the XML file populates the datagridview, however I can't find a way to do it.

(Using a basic setup in my application) I open the XML file in Google Chrome, it looks like this (the layout isn't perfect, but I can change it later):

<Root_Element>
<Output_Port Output_Baud="9600">COM104</Output_Port>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM3</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM1</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM6</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM5</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM4</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is On" Baud="9600" Extract="12345" Data="">COM100</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM101</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is On" Baud="9600" Extract="" Data="">COM102</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM103</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM104</Serial_Port_Name>
<Serial_Port_Name Use="Checkbox is Off" Baud="9600" Extract="" Data="">COM105</Serial_Port_Name>
</Root_Element>

The code below is how I am saving the data. I just want to be able to essentially do the reverse of this, and put the data back to where it came from.

private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {

        // Create the XmlDocument.
        XmlDocument doc = new XmlDocument();

        // Create root element.
        XmlElement rootElem = doc.CreateElement("Root_Element");
        doc.AppendChild(rootElem);

        // Add an "Output Port" element.                
        XmlElement outPortElem = doc.CreateElement("Output_Port");
        outPortElem.Value = (string)comboBox1.SelectedItem;
        rootElem.AppendChild(outPortElem);

        // Add an "Output Baud" Attribute.
        XmlAttribute outBaudAtt = doc.CreateAttribute("Output_Baud");
        outBaudAtt.Value = (string)comboBox2.SelectedItem;
        outPortElem.Attributes.Append(outBaudAtt);

        foreach (DataGridViewRow row in dataGridView1.Rows)
        {
            var ip_port2 = (SerialPort)row.Tag;

            // Write Serial Port Name element.
            XmlElement nameElem = doc.CreateElement("Serial_Port_Name");
            nameElem.Value = (string)row.HeaderCell.Value; // display the port name
            rootElem.AppendChild(nameElem);

            if (ip_port2.IsOpen == true) // if the ip_port is open
            {
                // Add a "Use" attribute.
                XmlAttribute useAtt = doc.CreateAttribute("Use");
                useAtt.Value = "Checkbox is On"; // display the port as "on"
                nameElem.Attributes.Append(useAtt);
            }
            else if (ip_port2.IsOpen != true) // if the ip_port is closed
            {
                // Add a "Use" attribute.
                XmlAttribute useAtt = doc.CreateAttribute("Use");
                useAtt.Value = "Checkbox is Off"; // display the port as "off"
                nameElem.Attributes.Append(useAtt);
            }

            // Add a "Baud" attribute.
            XmlAttribute baudAtt = doc.CreateAttribute("Baud");
            baudAtt.Value = (string)row.Cells[1].Value;
            nameElem.Attributes.Append(baudAtt);

            // Add an "Extract" attribute.
            XmlAttribute extractAtt = doc.CreateAttribute("Extract");
            extractAtt.Value = (string)row.Cells[2].Value;
            nameElem.Attributes.Append(extractAtt);

            // Add a "Data" attribute.
            XmlAttribute dataAtt = doc.CreateAttribute("Data");
            dataAtt.Value = (string)row.Cells[3].Value;
            nameElem.Attributes.Append(dataAtt);
        }

        // Save the document to a file. White space is preserved (no white space).
        doc.PreserveWhitespace = true;            
        doc.Save(saveFileDialog1.FileName);

Any help would be great! Thanks in advance!

Try to use DataSet/DataTable .

This code reads your xml file:

var ds = new DataSet();
ds.ReadXml("filename.xml");

Now there are two DataTable in the DataSet .

Let's get values from the first:

var outputBaud = ds.Tables[0].Rows[0]["Output_Baud"];
var outputPort = ds.Tables[0].Rows[0]["Output_Port_Text"];

Next, fill DataGridView from the second DataTable (Attention! Black magic!):

dataGridView.DataSource = ds.Tables[1];

Simple, isn't it?

Now you can edit values in the DataGridView . Thanks to data binding, they will be stored in the DataTable .

Saving data is very simple too.

ds.Tables[0].Rows[0]["Output_Baud"] = outputBaud;
ds.Tables[0].Rows[0]["Output_Port_Text"] = outputPort;

ds.WriteXml("filename.xml");

You can throw away your bulky code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM