【问题标题】:Show XML node value in MVC view by number of similar node?按相似节点的数量在 MVC 视图中显示 XML 节点值?
【发布时间】:2015-07-08 13:19:08
【问题描述】:

有没有将 xml 节点值按相似的数字分组?我试图输出如下的值,但我无法得到我想要的输出。请指导我。谢谢!

我在数据库中的 Answer_Data 样本:(提取后会得到 C1 和 C2)

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

使用 Linq 提取后的 String[] 数据:

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

能够在 MVC 中查看:

c1
c2
c2

我想要的是:

c1 - 1
c2 - 2

我的控制器:

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);
                }
        }

我的观点:

 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/>
            }
       }

【问题讨论】:

  • 试试这个代码 var result = (from item in viewModel.ResultAnswer item by item into itemGroup select String.Format("{0} - {1}", itemGroup.Key, itemGroup.Count ()));回复stackoverflow.com/questions/9702693/…

标签: c# asp.net-mvc-4 for-loop razor linq-to-xml


【解决方案1】:

正如您所说的那样,您正在接收元素数组,而不是像这样尝试 group by

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()
             }

像这样使用 linq 尝试分组

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()
             }

【讨论】:

  • @Edward.K - 你是否以 xml 形式获取数据
  • 嗨@pranay rana,不,我通过查询从数据库获取数据。列类型为 xml。当我插入数据时,我也将其转换为 xml 格式。
  • 我更新了我的问题以可视化数据。对于“您的 XML 字符串”,它是示例“C1”吗?
  • XmlDocument != XDocument。这不会编译。你想要XDocument.Parse
  • @Edward.K - 检查答案可能对您有所帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多