1

私はこのようなxmlを持っています

<rootnode>

<lvl1>AIT</lvl1>

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

</rootnode>

以下のxml解析コード

public class accountlist
        {
            public string lvl1 { get; set; }
            public List<string> b { get; set; }                
        } 

List<accountlist> questions = (from c in xmlDoc.Descendants("rootnode")
                                           select new accountlist
                                        {
                                            lvl1 = c.Element("rootnode").Value,
                                            b = (from q in c.Descendants("lvl2").Elements("b").Elements("a") where q.Element("a").Value == "0"
                                                             select q.Value).ToList(),
                                        }).ToList();

私の出力は次のようになります

lvl1 = AIT
b = 111

私を助けてください......

4

1 に答える 1

1

次のクエリを試してください。

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

lvl1 と b の単純な値が必要な場合は、結果セットで b のリストを取得しようとしているようです。

 var question2 = from c in doc.DescendantsAndSelf("rootnode")
                            select new
                            {
                                lvl1 = c.Element("lvl1").Value,
                                b =  c.Descendants("lvl2").Elements("b").FirstOrDefault().Value
                            };
于 2012-05-28T09:02:39.413 に答える