How to import XML file with varying number of nodes and child nodes into DataTable in C#? -


i have following xml file need import datatable in c#:

<?xml version="1.0"?> <data>     <systemid>         <information>         </information>     </systemid>     <measurement_data>         <channel_01>             <parameter_1 attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             <parameter_2 attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             <parameter_3 attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             .             .             .             <parameter_n attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />         </channel_01>         <channel_02>             <parameter_a attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             <parameter_b attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             <parameter_c attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             .             .             .             <parameter_n attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />         </channel_02>         .         .         .         <channel_z>             <parameter_1a attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />             <parameter_2a attribute1="double number" atribute2="string" attribute3="double number" attribute4="string" />             <parameter_3a attribute1="double number" atribute2="string" attribute3="double number" attribute4="string" />             .             .             .             <parameter_3n attribute1="double number" attribute2="string" attribute3="double number" attribute4="string" />         </channel_z>     </measurement_data> </data> 

here boundary conditions in file:

  1. the number of total channels varies depending on imported file.
  2. the number of parameters in each channel varies (both in name , total count).
  3. the parameters each channel not same.
  4. each parameter has same 4 attributes: 2 numbers , 2 strings.

i able load xml document xmldocument , create list of elements xelement , separately able generate datatable single parameter across multiple channels don't know how piece together.

here code generating datatable single parameter across multiple channels (note don't know how add channel information datatable yet):

private void openbutton_click(object sender, eventargs e) {     datatable values = new datatable();      values.columns.add("parameter");     values.columns.add("attribute1");     values.columns.add("attribute2");     values.columns.add("attribute3");     values.columns.add("attribute4");      string filepath = @"c:/users/.../test.xml";      list<string> parameter = new list<string>();     list<double> attribute1values = new list<double>();     list<string> attribute2values = new list<string>();     list<double> attribute3values = new list<double>();     list<string> attribute4values = new list<string>();      xmldocument doc = new xmldocument();     doc.load(filepath);      xmlnodelist elemlist = doc.getelementsbytagname("parameter_1");     string param = "parameter_1";      (int = 0; < elemlist.count; i++)     {         parameter.add(param);         attribute1values.add(double.parse(elemlist[i].attributes["attribute1"].value));         attribute2values.add(elemlist[i].attributes["attribute2"].value);         attribute3values.add(double.parse(elemlist[i].attributes["attribute3"].value));         attribute4values.add(elemlist[i].attributes["attribute4"].value);     }      (int = 0; < attribute1values.count;i++ )     {         var row = values.newrow();         row["parameter"] = parameter[i];         row["attribute1"] = attribute1values[i];         row["attribute2"] = attribute2values[i];         row["attribute3"] = attribute3values[i];         row["attribute4"] = attribute4values[i];         values.rows.add(row);     }     datagridview1.datasource = values; } 

try solution:

private void openbutton_click(object sender, eventargs e) {     datatable values = new datatable();      values.columns.add("channel");     values.columns.add("parameter");     values.columns.add("attribute1");     values.columns.add("attribute2");     values.columns.add("attribute3");     values.columns.add("attribute4");      string filepath = @"c:/users/.../test.xml";     xdocument xdoc = xdocument.load(filepath);      var channels = channel in xdoc.descendants("measurement_data").elements()                    select new                    {                        channelname = channel.name,                        parameters = channel.elements().select(a => new                        {                            parametername = a.name,                            attribute1 = a.attribute("attribute1").value,                            attribute2 = a.attribute("attribute2").value,                            attribute3 = a.attribute("attribute3").value,                            attribute4 = a.attribute("attribute4").value                        })                     };      foreach (var channel in channels)     {         foreach (var element in channel.parameters)         {             datarow row = values.newrow();              row["channel"] = channel.channelname;             row["parameter"] = element.parametername;             // if attributes not numbers, parsing generate error.             row["attribute1"] = double.parse(element.attribute1);             row["attribute2"] = element.attribute2;             row["attribute3"] = double.parse(element.attribute3);             row["attribute4"] = element.attribute4;              values.rows.add(row);         }     }     datagridview1.datasource = values; } 

i tested following xml document:

<?xml version="1.0"?> <data>     <systemid>         <information>         </information>     </systemid>     <measurement_data>         <channel_01>             <parameter_1 attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_2 attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_3 attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_n attribute1="123" attribute2="string" attribute3="456" attribute4="string" />         </channel_01>         <channel_02>             <parameter_a attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_b attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_c attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_n attribute1="123" attribute2="string" attribute3="456" attribute4="string" />         </channel_02>         <channel_z>             <parameter_2a attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_3a attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_1a attribute1="123" attribute2="string" attribute3="456" attribute4="string" />             <parameter_3n attribute1="123" attribute2="string" attribute3="456" attribute4="string" />         </channel_z>     </measurement_data> </data> 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -