简体   繁体   中英

How to read XML file and display in datagridview in C#

I am developing a C# application and trying to read XML file present in one directory and display the records in DataGridView..

What I have tried:

    DataSet ds = new DataSet();
    ds.ReadXml(@"G:\\Campaign_20062601.xml");
    dataGridView1.DataSource = ds.Tables[0]; 

this code work fine but it show only <DIP_1> data

here is xml file..

<CAMPAIGN_00>

<DIP_1>
<stSetpoints>
<iInputCatsPerTooling>8</iInputCatsPerTooling>
<rInputSPSubstrateWeight>
<fMin>126</fMin>
<fMax>154</fMax>
<fTgt>140</fTgt>
</rInputSPSubstrateWeight>
<rInputSPWetUptakeWeight>
<fMin>8.1000004</fMin>
<fMax>9.8999996</fMax>
<fTgt>9</fTgt>
</rInputSPWetUptakeWeight>
<rCalculatedSPWetUptakePerc>
<fMin>90</fMin>
<fMax>109.99999</fMax>
<fTgt>100</fTgt>
</rCalculatedSPWetUptakePerc>
</stSetpoints>
<sDipStatus>GOOD</sDipStatus>
<rSubstrateWeight>1144</rSubstrateWeight>
<rWetScale1>1217.9</rWetScale1>
<rWetUptakeWeight>73.900024</rWetUptakeWeight>
<rWetUptakePercent>102.63892</rWetUptakePercent>
</DIP_1>

<DIP_2>
<stSetpoints>
<iInputCatsPerTooling>8</iInputCatsPerTooling>
<rInputSPSubstrateWeight>
<fMin>126</fMin>
<fMax>154</fMax>
<fTgt>140</fTgt>
</rInputSPSubstrateWeight>
<rInputSPWetUptakeWeight>
<fMin>8.1000004</fMin>
<fMax>9.8999996</fMax>
<fTgt>9</fTgt>
</rInputSPWetUptakeWeight>
<rCalculatedSPWetUptakePerc>
<fMin>90</fMin>
<fMax>109.99999</fMax>
<fTgt>100</fTgt>
</rCalculatedSPWetUptakePerc>
</stSetpoints>
<sDipStatus>GOOD</sDipStatus>
<rSubstrateWeight>1143.4</rSubstrateWeight>
<rWetScale1>1222.1</rWetScale1>
<rWetUptakeWeight>78.699951</rWetUptakeWeight>
<rWetUptakePercent>109.30549</rWetUptakePercent>
</DIP_2>

.
.
.
</CAMPAIGN_00>

i need below output in datagridview..

GOOD    1144    1217.9  73.900024   102.63892
GOOD    1143.4  1222.1  78.699951   109.30549

any idea please help...

DataSet is limited to 4 levels of tags. You have more so use xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;

namespace ConsoleApplication166
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name", typeof(string));
            dt.Columns.Add("sDipStatus", typeof(string));
            dt.Columns.Add("rSubstrateWeight", typeof(decimal));
            dt.Columns.Add("rWetScale1", typeof(decimal));
            dt.Columns.Add("rWetUptakeWeight", typeof(decimal));
            dt.Columns.Add("rWetUptakePercent", typeof(decimal));

            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement dip in doc.Root.Elements())
            {
                dt.Rows.Add(new object[] {
                    dip.Name.LocalName,
                    (string)dip.Element("sDipStatus"),
                    (decimal)dip.Element("rSubstrateWeight"),
                    (decimal)dip.Element("rWetScale1"),
                    (decimal)dip.Element("rWetUptakeWeight"),
                    (decimal)dip.Element("rWetUptakePercent")
                });
            }

        }
    }
 
}

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