如何在asp.net的某个特定位置将Xelement的Inner Xelements添加到其他XElement

时间:2017-12-12 06:34:33

标签: xelement

这是我创建的Xelement(“elementExamQuestion”)。

XElement elementExamQuestion = new XElement("ExamQuestions",
                                                                                        new XAttribute("id", xmodel.examQuestionID),
                                                                                        new XAttribute("srno", xmodel.examqsrno),                                                                                
                                                                                        new XAttribute("isnegative", masterquesResult.qisnegative == null ? "0" : masterquesResult.qisnegative.ToString()),
                                                                                        new XAttribute("quesstatus", "NV"),                                                                                          
                                                                                    new XElement("QuestionId", dr.questionid.ToString()),
                                                                                    new XElement("QuestionNo", ""),
                                                                                    new XElement("Question", dr.question.ToString().Trim()),

                                                                                **<----At This position----->**

                                                                                 new XElement("TopicId",dr.topicid.ToString()),
                                                                                    new XElement("ExamId", xmodel.GI.CE_examid.ToString().Trim()),                                                                                       
                                                                                    new XElement("TimeTaken",""),
                                                                                    new XElement("UTCQuesTime","")
                                                                                                );

现在是其他Xelement(“选项”)

     XElement options = new XElement("options",
                                                        new XElement("OptionA",new XAttribute("value","a"),dr.a.Trim()),
                                                        new XElement("OptionB",new XAttribute("value","b"),dr.b.Trim()),
                                                        new XElement("OptionC",new XAttribute("value","c"),dr.c.Trim()),
                                                        new XElement("OptionD",new XAttribute("value","d"),dr.d.Trim()),
                                                        new XElement("OptionE",new XAttribute("value","e"),dr.e.Trim()),
                                                        new XElement("OptionF",new XAttribute("value","f"),dr.f.Trim()),
                                                        new XElement("OptionG",new XAttribute("value","g"),dr.g.Trim()),
                                                        new XElement("OptionH",new XAttribute("value","h"),dr.h.Trim())
                                                        );

我想在Xelement(“elementExamQuestion”)中插入Xelements optionA,OptionB,Option C到Option H,但是在XElement(“Question”)之后。

如果有人能帮助我,那就太好了。在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

经过研究,我得到了精确的解决方案

 elementExamQuestion.Descendants("Question").ElementAt(0).AddAfterSelf(options.Elements());