C# 4.0 在c中使用linq解析xml元素时遇到问题#

C# 4.0 在c中使用linq解析xml元素时遇到问题#,c#-4.0,linq-to-xml,C# 4.0,Linq To Xml,我有一个这样的xml <rootnode> <lvl1>AIT</lvl1> <lvl2> <a>0</a> <b> 111</b> </lvl2> </rootnode> lvl1 = AIT b = 111 请帮助我……尝试以下查询 var doc = XElement.Parse(@"<rootnode><lvl1>AIT<

我有一个这样的xml

<rootnode>

<lvl1>AIT</lvl1>

<lvl2>
<a>0</a>
<b> 111</b> 
</lvl2>

</rootnode>
lvl1 = AIT
b = 111

请帮助我……

尝试以下查询

    var doc = XElement.Parse(@"<rootnode><lvl1>AIT</lvl1><lvl2><a>0</a><b> 111</b> </lvl2></rootnode>");

    var questions = from c in doc.DescendantsAndSelf("rootnode")
                    select new accountlist
                               {
                                   lvl1 = c.Element("lvl1").Value,
                                    b = (from q in c.Descendants("lvl2") where q.Element("a").Value == "0"
                                                     select q.Element("b").Value).ToList()
                               };

请尝试以下查询

    var doc = XElement.Parse(@"<rootnode><lvl1>AIT</lvl1><lvl2><a>0</a><b> 111</b> </lvl2></rootnode>");

    var questions = from c in doc.DescendantsAndSelf("rootnode")
                    select new accountlist
                               {
                                   lvl1 = c.Element("lvl1").Value,
                                    b = (from q in c.Descendants("lvl2") where q.Element("a").Value == "0"
                                                     select q.Element("b").Value).ToList()
                               };
 var question2 = from c in doc.DescendantsAndSelf("rootnode")
                            select new
                            {
                                lvl1 = c.Element("lvl1").Value,
                                b =  c.Descendants("lvl2").Elements("b").FirstOrDefault().Value
                            };