在MVC视图中按相似节点的数目显示XML节点值
本文关键字:节点 显示 XML 视图 相似 MVC | 更新日期: 2023-09-27 18:01:41
是否有类似的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提取后的字符串[]数据:
["c1","c2","c2"]
可以在MVC中查看:
c1
c2
c2
我要的是:
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);
}
}
我的观点:
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/>
}
}
当你在问题中声明你正在接收元素数组时,你只需像这样尝试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()
}
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()
}