XML multiple nodes into dataGridView with 2 column

Viewed 203

I'm trying to put this xml into a DataGridView with the data grid view only showing the nodes and the value.

I have tried using switch, case, foreach node yet the result the node and value only showing on 1 column instead 2 column. I'm tried using select single node, but it showing object error.,

Here's an example of the code:

try
            {
                reader = new XmlTextReader(filename);
                reader.WhitespaceHandling = WhitespaceHandling.None;
               

                while (reader.Read())
                {
                    

                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            //dataGridView1.Rows[cnt].Cells[1].Value = reader.Name;
                            dataGridView1.Rows.Add(reader.Name);
                            break;
                        case XmlNodeType.Text:
                            dataGridView1.Rows.Add("\t",reader.Value);
                            //dataGridView1.Rows[cnt].Cells[0].Value = reader.Value;
                            break;
                        case XmlNodeType.EndElement:
                            break;
                    }

my Mentor said, use this code but i dont know what's missing with my mentor example

XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("ConfigAPI.xml");

            XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("/ConfigAPI");
            
               foreach (XmlNode node in nodeList)
            {
            node.SelectSingleNode("UIPlayer").InnerText;
            }

also here's an example of the XML

<ConfigAPI>
  <CoreUIAddress>http://localhost:3223</CoreUIAddress>
  <CoreAppsAddress>http://localhost:3333</CoreAppsAddress>
  <CoreCustomPrinterAddress>http://localhost:3001</CoreCustomPrinterAddress>
  <CorePassbookPrinterAddress>http://localhost:3002</CorePassbookPrinterAddress>
  <CorePrinterAddress>http://localhost:3003</CorePrinterAddress>
  <CoreEDCAddress>http://localhost:3010</CoreEDCAddress>
  <CoreCamAddress>http://localhost:3011</CoreCamAddress>
  <CoreUPSAddress>http://localhost:3012</CoreUPSAddress>
</ConfigAPI>

by using the first code, it shown something like this (without "\t")

Column 1                       Column 2
core UI Adress
http localhost
core APps
http localhost

not sure how to shown the data on 2nd Column so it can be view like this

Column 1                      Column 2
Core Ui Adress                 Http Localhost
Core APPs                      Http Localhost
1 Answers

Using the few lines of code in the second example posted and the given XML… Below should display the data is two columns.

  XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load(@"PathToYourXMLFile\ConfigAPI.xml");
  XmlNodeList RootNodeList = xmlDoc.DocumentElement.SelectNodes("/ConfigAPI");
  for (int i = 0; i < 2; i++) {
    dataGridView1.Columns.Add(new DataGridViewTextBoxColumn());
  }
  foreach (XmlNode ConfigNode in RootNodeList[0]) {
    foreach (XmlNode childNode in ConfigNode.ChildNodes) {
      int newRowIndex = dataGridView1.Rows.Add();
      dataGridView1.Rows[newRowIndex].Cells[0].Value = childNode.ParentNode.Name;
      dataGridView1.Rows[newRowIndex].Cells[1].Value = childNode.InnerText;
    }
  }

Addition per OP comment...

It is a little confusing in a sense that the first “outer” loop isn’t really necessary using the posted XML. When I tested the code, I had numerous ConfigAPI nodes in the XML file. Not just the one as you have posted. So, you could simplify this to one loop with…

foreach (XmlNode childNode in RootNodeList[0].ChildNodes) {
  int newRowIndex = dataGridView1.Rows.Add();
  dataGridView1.Rows[newRowIndex].Cells[0].Value = childNode.Name;
  dataGridView1.Rows[newRowIndex].Cells[1].Value = childNode.InnerText;
}
Related