此代码对我不起作用,我无法理解原因。
xmlDoc.Element("results").Add(
new XElement("user",
new XAttribute("id", user.Id),
new XAttribute("facebookid", user.FacebookId),
new XAttribute("email", user.Email),
new XAttribute("totalpoints", totalpoints)
).Add(
user.Answers.Select(value => new XElement("question",
new XAttribute("id", value.QuestionId),
new XAttribute("answer_id", value.AnswerId),
new XAttribute("points", value.Points)))
)
);
我正在尝试创建此
<results>
<user id="2323" facebookId="3254954795743957" email="david@gmail" totalPoints="">
<question id="1" answer_id="3" points="0" />
<question id="2" answer_id="1" points="1" />
</user>
</results>
答案 0 :(得分:1)
xmlDoc.Element("results").Add(
new XElement("user",
new XAttribute("id", user.Id),
new XAttribute("facebookid", user.FacebookId),
new XAttribute("email", user.Email),
new XAttribute("totalpoints", totalpoints),
user.Answers.Select(value => new XElement(
"question",
new XAttribute("id", value.QuestionId),
new XAttribute("answer_id", value.AnswerId),
new XAttribute("points", value.Points)
))
)
);
问题在于Add
方法会返回void
,因此您无法继续使用Add
结果上的void
。此外,对于属性,您需要使用XAttribute
,而不是XElement
。