Loping through multiple elements of same Name XML [Resolved]

Posted by Self-Innovator under XML on 4/24/2016 | Points: 10 | Views : 393 | Status : [Member] | Replies : 2
Hi i have an XML file where i need to bind some attribute values to my class property

with the below XML i want to bind

storeinfo.BusinessEmail Property with XML attribute

<memberpreference preference_category="E-Mail Preferences" preference_item_code="MEM" preference_item_desc="E-mail Address for Consumers" preference_specified="Yes" preference_value="ahmed.k@gmai.com" />

also i wanted to bind storeInfo.Website

<memberpreference preference_category="Web Site Preferences" preference_item_code="URL" preference_item_desc="URL Address for Member Store" preference_specified="Yes" preference_value="http://www.truevalue.com/trusitedemo" />

How can i achieve this


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

<memberpreference preference_category="Broadcast Message To E-Mail Preferences" preference_item_code="XEC" preference_item_desc="Corporate Communications" preference_specified="Yes" preference_value="ahmed.k@gmail.com" />

<memberpreference preference_category="Broadcast Message To E-Mail Preferences" preference_item_code="XEX" preference_item_desc="Executive Communications" preference_specified="Yes" preference_value="ahmed.k@gmail.com" />

<memberpreference preference_category="Broadcast Message To Fax Preferences" preference_item_code="XFP" preference_item_desc="Sales And Promotions" preference_specified="No" preference_value="" />

<memberpreference preference_category="E-Mail Preferences" preference_item_code="OEA" preference_item_desc="COO E-mail" preference_specified="No" preference_value="" />

<memberpreference preference_category="E-Mail Preferences" preference_item_code="DFM" preference_item_desc="Default E-mail Address" preference_specified="No" preference_value="" />

<memberpreference preference_category="E-Mail Preferences" preference_item_code="MEM" preference_item_desc="E-mail Address for Consumers" preference_specified="Yes" preference_value="ahmed.k@gmai.com" />

<memberpreference preference_category="E-Mail Preferences" preference_item_code="OCE" preference_item_desc="Order Confirmation Notification E-Mail address" preference_specified="Yes" preference_value="vkumar1@truevalue.com" />

<memberpreference preference_category="E-Mail Preferences" preference_item_code="ONP" preference_item_desc="Order Notification Primary E-mail" preference_specified="Yes" preference_value="eric.tuttleman@truevalue.com" />

<memberpreference preference_category="Store Locator Preferences" preference_item_code="W" preference_item_desc="Address Line 1" preference_specified="Yes" preference_value="308 S Division St" />

<memberpreference preference_category="Store Locator Preferences" preference_item_code="W" preference_item_desc="City Name" preference_specified="Yes" preference_value="Harvard" />

<memberpreference preference_category="Web Site Preferences" preference_item_code="DFU" preference_item_desc="Default URL Address" preference_specified="No" preference_value="" />

<memberpreference preference_category="Web Site Preferences" preference_item_code="FCB" preference_item_desc="Facebook URL Address" preference_specified="No" preference_value="" />

Join Hands Change lives
Thanks & Regards
Straight Edge Society



Responses

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

Up
0
Down

Resolved
Hi,
Earlier also you have asked similar kind of questions and we have already provided the answer.

Those are here ( http://www.dotnetfunda.com/forums/show/21339/looping-through-xml-attributes-multiple-columns ) and here ( http://www.dotnetfunda.com/forums/show/21320/getting-null-in-xml-attribute ).

What we suggest is that, please try by yourself so that you can learn.

After your try, if you fail to solve, then we will definitely do it for for you.

Hope you understand.

Thanks in advance.

--
Thanks & Regards,
RNA Team

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

Posted by: Self-Innovator on: 4/25/2016 [Member] Bronze | Points: 25

Up
0
Down
Hi Raj,

Thanks for your response ,I have applied your solution on my previous post but i wanted to use XmlDocument to load the XML file instead of XDocument, the below is my XML file
<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>
I have tried like this but i'm not getting all the results i'm unable to loop through the attributes ,
  XmlDocument xmlDoc = new XmlDocument();

string StatementReportResponseXml = reportService.GetStatementReportInfo(storeId, currentMonth.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("ns", "http://wsdev.truserv.com/oraclewebservice/oraclemdb.asmx");
XmlNode resultNode = xmlDoc.SelectSingleNode("results", nsmgr);
XmlNode userNodeTable = resultNode.SelectSingleNode("statements/table", nsmgr);
if (userNodeTable != null)
{
statementsReport.TableName = userNodeTable.Attributes["table_name"] != null ? userNodeTable.Attributes["table_name"].Value : string.Empty;
XmlNode userNodeColumnHeaders = resultNode.SelectSingleNode("statements/table/table_col_headings", nsmgr);
statementsReport.CoulmnHeading4 = userNodeColumnHeaders.Attributes["col_heading_4"] != null ? userNodeColumnHeaders.Attributes["col_heading_4"].Value : string.Empty;
statementsReport.CoulmnHeading5 = userNodeColumnHeaders.Attributes["col_heading_5"] != null ? userNodeColumnHeaders.Attributes["col_heading_5"].Value : string.Empty;
statementsReport.CoulmnHeading6 = userNodeColumnHeaders.Attributes["col_heading_6"] != null ? userNodeColumnHeaders.Attributes["col_heading_6"].Value : string.Empty;
statementsReport.CoulmnHeading7 = userNodeColumnHeaders.Attributes["col_heading_7"] != null ? userNodeColumnHeaders.Attributes["col_heading_7"].Value : string.Empty;
statementsReport.CoulmnHeading8 = userNodeColumnHeaders.Attributes["col_heading_8"] != null ? userNodeColumnHeaders.Attributes["col_heading_8"].Value : string.Empty;
XmlNode userNodeTableRows = resultNode.SelectSingleNode("statements/table/table_rows/table_row", nsmgr);
statementsReport.Stock = userNodeTableRows.Attributes["row_caption"] != null ? userNodeTableRows.Attributes["row_caption"].Value : string.Empty;
}

Basically i need an output which i have shared as an attachment kindly provide me a solution
 Download source file

Join Hands Change lives
Thanks & Regards
Straight Edge Society

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

Login to post response