Parse a xml file for multiple rows and columns using XDocument and C#

Rajnilari2015
Posted by Rajnilari2015 under C# category on | Points: 40 | Views : 536
Let's say we have an XM file as under

<results>
<response mode="all" value=""/>
<statements>

<table table_name="Year To Date (Lumber/TVM Paint are included in Total Sales)">
<table_col_headings col_heading_4="Thru Mar 2016" col_heading_5="Var %" col_heading_6="Thru Mar 2015" col_heading_7="Var %" col_heading_8="Thru Mar 2014" />
<table_rows>
<table_row row_caption="Stock" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Event" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Handled" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Direct Ship" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Total Sales" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Lumber" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="TVM Paint" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
</table_rows>
</table>

<table table_name="Period End (Lumber/TVM Paint are included in Total Sales)">
<table_col_headings col_heading_4="Mar 2016" col_heading_5="Var %" col_heading_6="Mar 2015" col_heading_7="Var %" col_heading_8="Mar 2014" />
<table_rows>
<table_row row_caption="Stock" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Event" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Handled" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Direct Ship" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Total Sales" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="Lumber" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
<table_row row_caption="TVM Paint" col_data4="0" col_data5="0" col_data6="0" col_data7="0" col_data8="0" />
</table_rows>
</table>
</statements>
</results>


And we have to parse it.The below is the code for doing so

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

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"test.xml");

var tblCollection = new List<Table>();

foreach (var tableElement in doc.Descendants("table"))
{
var tbl = new Table();

tbl.TblName = tableElement.Attribute("table_name").Value; //set the table name

var tblColHeadings = new TableColHeadings();

//loop for the table_col_headings
foreach (var tableCOLElement in tableElement.Descendants("table_col_headings"))
{
tblColHeadings.ColHeading4 = tableCOLElement.Attribute("col_heading_4").Value;
tblColHeadings.ColHeading5 = tableCOLElement.Attribute("col_heading_5").Value;
tblColHeadings.ColHeading6 = tableCOLElement.Attribute("col_heading_6").Value;
tblColHeadings.ColHeading7 = tableCOLElement.Attribute("col_heading_7").Value;
tblColHeadings.ColHeading8 = tableCOLElement.Attribute("col_heading_8").Value;

tbl.TblColHeadings = tblColHeadings; //set the column headings
}

var tblRows = new TableRows();
tblRows.TblRow = new List<TableRow>();

//loop for the table_row
foreach (var tableROWData in tableElement.Descendants("table_row"))
{
var tblRow = new TableRow();
tblRow.row_caption = tableROWData.Attribute("row_caption").Value;
tblRow.col_data4 = tableROWData.Attribute("col_data4").Value;
tblRow.col_data5 = tableROWData.Attribute("col_data5").Value;
tblRow.col_data6 = tableROWData.Attribute("col_data6").Value;
tblRow.col_data7 = tableROWData.Attribute("col_data7").Value;
tblRow.col_data8 = tableROWData.Attribute("col_data8").Value;

tblRows.TblRow.Add(tblRow);
}

tbl.TblRows = tblRows; //set the table Rows

//add to the Table collection
tblCollection.Add(tbl);
}
}
}

public class TableRow
{
public string row_caption { get; set; }
public string col_data4 { get; set; }
public string col_data5 { get; set; }
public string col_data6 { get; set; }
public string col_data7 { get; set; }
public string col_data8 { get; set; }
}

public class TableColHeadings
{
public string ColHeading4 { get; set; }
public string ColHeading5 { get; set; }
public string ColHeading6 { get; set; }
public string ColHeading7 { get; set; }
public string ColHeading8 { get; set; }
}

public class TableRows
{
public List<TableRow> TblRow { get; set; }
}

public class Table
{
public string TblName { get; set; } //store table name
public TableColHeadings TblColHeadings { get; set; } //store column headings info
public TableRows TblRows { get; set; } //stores table rows info
}

}

Comments or Responses

Login to post response