简体   繁体   English

无法使用linq将数据从列表添加到xml

[英]Cant add data to xml from list using linq

This code doesn't work for me and I can't understand why. 此代码对我不起作用,我也不明白为什么。

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)))
                          )
                  );

I'm trying to create this 我正在尝试创建这个

<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>
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)
            ))
        )
    );

The problem was that the Add method returns void so you can't continue with another Add over the void result. 问题在于Add方法返回void因此您不能在void结果上继续执行另一个Add Also, for attributes you need to use XAttribute , not XElement . 另外,对于属性,您需要使用XAttribute ,而不是XElement

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM