Show XML node value in MVC view by number of similar node?

Edward.K

Is there anyway to group the xml node value by similar into number? I had tried to output the value as below but i can't get the out as what i want. Please guide me on this. Thanks!

My Answer_Data Sample in database: (After extract will get the C1 and C2)

Question_ID |  Answer_Data
==============================================
    1       | <Answer_Data><Answer>C1</Answer>
    2       | <Answer_Data><Answer>C2</Answer>
    3       | <Answer_Data><Answer>C2</Answer>

String[] data after extract using Linq:

["c1","c2","c2"]

Able to View in MVC:

c1
c2
c2

What i want is:

c1 - 1
c2 - 2

My Controller:

public ActionResult SURV_Answer_Result(int Survey_ID, string Language = "ENG")
        {
        List<AnswerQuestionViewModel> viewmodel = new List<AnswerQuestionViewModel>();

                    var query = from r in db.SURV_Question_Ext_Model
                                join s in db.SURV_Question_Model
                                on r.Qext_Question_ID equals
                                s.Question_ID
                                select new { r, s };

                    var viewModel = new AnswerQuestionViewModel();
                    viewModel.Survey_ID = Survey_ID;

                    string[] resultanswer = new string[queryResult.Count()];

                    foreach (var item in query.ToList())
                    {

                        string str = item.s.Answer_Data;
                        XElement qconfig;
                        qconfig = XElement.Parse(str);

                        string value = item.s.Question_Type;
                        int i = 0;

                        switch (value)
                        {

                             case "Choices":
                                {
                                    XElement ChoicesType =
                                    (from node in qconfig.Elements("ChoicesType")
                                     select node).SingleOrDefault();

                                    viewModel.ChoiceType = ChoicesType.Value;

                                    XElement ChoicesAns =
Here is i get the answer data ===>> (from node in qconfig.Elements("Answer")
                                    select node).SingleOrDefault();

                                    resultanswer[i++] = ChoicesAns.Value;
                                    viewModel.ResultAnswer = resultanswer;

                                }
                                break;

                            case "Multiple_Line":
                                {
                                   // do nothing
                                }
                                break;



                        viewmodel.Add(new AnswerQuestionViewModel()
                        {              
                            ResultAnswer = viewModel.ResultAnswer        
                        });
                    }

                    return View(viewmodel);
                }
        }

My View:

 if (Model[i].ChoiceType == "SingleChoice")
               {
                for (int x = 0; x < Model[i].ResultAnswer.Count(); x++)
                {

               @Html.LabelFor(m => m[i].Answer, Model[i].ResultAnswer[x].ToString(),new{ @class="qlabel2" })

                <br/>
            }
       }
Pranay Rana

As you are stating in question that you are receiving array of element than you just try group by like this

string[] strarray = new string[] {"c1","c2","c2"};
var groups = from str in strarray 
             group str by str into g
             select new {
               key = g.Key,
               count = g.Count()
             }

try group by using linq like this

var xmlstr="<root><Answer_Data><Answer>C1</Answer></Answer_Data>
<Answer_Data><Answer>C2</Answer></Answer_Data>
<Answer_Data><Answer>C2</Answer></Answer_Data></root>";

XDocument xmldoc = XDocument.Parse(xmlstr);

var groups = from record in xmldoc.Descendants("Answer_Data")
             group record by (string)record.Element("Answer")  
             into g
             select new {
               key = g.Key,
               count = g.Count()
             }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related