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)
                    ).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>
4

1 回答 1

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因此您无法继续使用另一个结果。此外,对于您需要使用的属性,而不是.AddvoidXAttributeXElement

于 2012-08-21T10:05:19.233 回答