3

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/>
            }
       }
1
  • try with this code var result = (from item in viewModel.ResultAnswer group item by item into itemGroup select String.Format("{0} - {1}", itemGroup.Key, itemGroup.Count())); answer in stackoverflow.com/questions/9702693/… Commented Jul 8, 2015 at 14:20

1 Answer 1

2

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()
             }
Sign up to request clarification or add additional context in comments.

6 Comments

@Edward.K - are you gettting data in xml form
hi @pranay rana, nope i getting data by query from database. The column type is in xml. When i insert data, i convert it to xml format too.
i had updated my question for visualize the data. For the "Your XML String", is it the sample "<Answer_Data><Answer>C1</Answer>" ?
XmlDocument != XDocument. This wouldn't compile. You want XDocument.Parse.
@Edward.K - check answer might help you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.