Looping through XML attributes Multiple columns [Resolved]

Posted by Self-Innovator under ASP.NET MVC on 4/20/2016 | Points: 10 | Views : 337 | Status : [Member] | Replies : 1
Hi I need to bind the model with some XML attributes with multiple columns dynamically, i have tried but it doesn't seems to workout, Appreciate your help My XML
<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>

Code behind
 XmlDocument xmlDoc = new XmlDocument();

string StatementReportResponseXml = _reportService.GetStatementReportInfo(StoreId, curMonth.ToString()).Result;
xmlDoc.LoadXml(StatementReportResponseXml);

var statementsReport = new StatementInfoViewModel();

//check for a an "error" node within the results
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace(&

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

Posted by: Rajnilari2015 on: 4/20/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
Try this

using System.Collections.Generic;

using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
XDocument doc = XDocument.Load(@"Input.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
}

}



--
Thanks & Regards,
RNA Team

Self-Innovator, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response