使用MVC 4中的循环动态添加XElement?

时间:2015-06-16 06:52:47

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

我想在Xdocument中添加Xelement但是如何使用循环动态添加? 我需要捕获用户多个答案并动态生成Xelement。 请指导我或提供任何提示。非常感谢!

    [HttpPost]
        public ActionResult SURV_Answer_Submit(List<AnswerQuestionViewModel> viewmodel, int Survey_ID, string Language)
        {
            if (ModelState.IsValid)
            {
                  var query = from r in db.SURV_Question_Ext_Model.ToList()
                        join s in db.SURV_Question_Model
                        on r.Qext_Question_ID equals
                        s.Question_ID
                        where  s.Question_Survey_ID == Survey_ID && r.Qext_Language == Language
                        orderby s.Question_Position ascending
                        select new { r, s };

                int i = 0;

                foreach(var item in query)
                {

                        var answer = new SURV_Answer_Model();
                        answer.Answer_Qext_ID = item.r.Qext_Question_ID;
                        string value = item.s.Question_Type;

                        string str = item.r.Qext_Configuration;

                        XElement qconfig;
                        qconfig = XElement.Parse(str);

                        XElement ChoicesType =
                        (from node in qconfig.Elements("ChoicesType")
                         select node).SingleOrDefault();

                         XDocument doc = new XDocument
                         (
                            new XDeclaration("1.0", "utf-8", "yes"),
                            new XComment("Question Configuration"),
                            new XElement("AnswerData",
                             new XElement("Answer1", viewmodel[i++].Answer),                    
                            new XElement("Answer2", viewmodel[i++].Answer), 
                            new XElement("Answer3", viewmodel[i++].Answer),
                            new XElement("Answer4", viewmodel[i++].Answer),
                            How to add dynamically for new XElement Answer2,3,4 here?


    );
                 answer.Answer_Data = doc.ToString();



                        db.SURV_Answer_Model.Add(answer);
                        db.SaveChanges();               
                }

                return RedirectToAction("SURV_Main_Index", "SURV_Main");

            }

            return View(viewmodel);
        }

3 个答案:

答案 0 :(得分:3)

尝试这样的事情:

XDocument doc = new XDocument
                 (
                       new XDeclaration("1.0", "utf-8", "yes"),
                           new XComment("Question Configuration"),
                           new XElement("AnswerData", GetAnswerData(viewmodel));

...

public List<XElement> GetAnswerData(Object viewmodel)
    {
        var result = new List<XElement>();

        for(int x =0 ; x < viewmodel[i].MultiAnswer.Count(); x++)
        {
            result.Add(new XElement("Answer",viewmodel[i++].MultiAnswer[x])));
        }
        return result;
    }

答案 1 :(得分:1)

这是完整的代码

[HttpPost]
public ActionResult SURV_Answer_Submit(List<AnswerQuestionViewModel> viewmodel, int Survey_ID, string Language)
{
    if (ModelState.IsValid)
    {
          var query = from r in db.SURV_Question_Ext_Model.ToList()
                join s in db.SURV_Question_Model
                on r.Qext_Question_ID equals
                s.Question_ID
                where  s.Question_Survey_ID == Survey_ID && r.Qext_Language == Language
                orderby s.Question_Position ascending
                select new { r, s };

        int i = 0;

        foreach(var item in query)
        {

                var answer = new SURV_Answer_Model();
                answer.Answer_Qext_ID = item.r.Qext_Question_ID;
                string value = item.s.Question_Type;

                string str = item.r.Qext_Configuration;

                XElement qconfig;
                qconfig = XElement.Parse(str);

                XElement ChoicesType =
                (from node in qconfig.Elements("ChoicesType")
                 select node).SingleOrDefault();

                 XDocument doc = new XDocument
                 (
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("Question Configuration"),
                    new XElement("AnswerData", GetAnswerData(viewmodel, i));

                answer.Answer_Data = doc.ToString();

                db.SURV_Answer_Model.Add(answer);
                db.SaveChanges();   
                i++;
        }

        return RedirectToAction("SURV_Main_Index", "SURV_Main");

    }

    return View(viewmodel);
}

public IEnumerable<XElement> GetAnswerData(List<AnswerQuestionViewModel> viewmodel, int i)
{
    for(int x =0 ; x < viewmodel[i].MultiAnswer.Count(); x++)
    {
        yield return new XElement("Answer",viewmodel[i++].MultiAnswer[x]));
    }
}

答案 2 :(得分:0)

来自answerOscar Fonseca的发烧友版本:

 XDocument doc = new XDocument
 (
       new XDeclaration("1.0", "utf-8", "yes"),
           new XComment("Question Configuration"),
           new XElement("AnswerData", GetAnswerData(viewmodel));
    //...

public IEnumerable<XElement> GetAnswerData(Object viewmodel)
{
    for(int x =0 ; x < viewmodel[i].MultiAnswer.Count(); x++)
    {
        yield return new XElement("Answer",viewmodel[i++].MultiAnswer[x]));
    }
}