0

私のxmlには次のものがあります。

  <mur>
       <bak>
       </bak> 
        <itemfb ident="c_fb">
            <flow_m>
                <mat>
                <text texttype="text/plain">correct answer comments</text>
                </mat>
            </flow_m>
        </itemfb>
        <itemfb ident="gc_fb">
            <flow_m>
                <mat>
                <text texttype="text/plain">wrong, you made a blunder</text>
                </mat>
            </flow_m>
        </itemfb>
  </mur>

現在、「itemfb」タグは「mur」タグ内に存在する場合と存在しない場合があります。存在する場合は、値「正解のコメント」(または)「間違っています、あなたは失敗しました」に応じて値を解析して取得する必要があります。 itemfb" ident. これが私が試したことです。rowObj に「mur」からロードされた xml があり、「ns」が名前空間であると仮定します。

            if (rowObj.Elements(ns + "itemfb").Any())
            {
                var correctfb = (from cfb in rowObj
                                .Descendants(ns + "itemfb")
                                where (string)cfb.Attribute(ns + "ident").Value == "cfb"
                                select new
                                { 
                                ilcfb = (string)cfb.Element(ns + "mat")
                                }).Single();

            some_variable_1 = correctfb.ilcfb;



                var incorrectfb = (from icfb in rowObj
                                .Descendants(ns + "itemfb")
                                where (string)icfb.Attribute(ns + "ident").Value == "gcfb"
                                select new 
                                { 
                                ilicfb = (string)icfb.Element(ns + "mat")
                                }).Single();

            some_variable_2 = incorrectfb.ilicfb;
            }
4

1 に答える 1

0

これは、必要な情報を取得する方法です。簡単にするために ns を省略しました。

var correctfb = rowObj.Descendants("mur")
   .Descendants("itemfb")
   .Where(e => e.Attribute("ident").Value == "c_fb")
   .Descendants("text").FirstOrDefault();

if (correctfb != null)
    some_variable_1 = correctfb.Value;

var incorrectfb = rowObj.Descendants("mur")
   .Descendants("itemfb")
   .Where(e => e.Attribute("ident").Value == "gc_fb")
   .Descendants("text").FirstOrDefault();

if (incorrectfb != null)
    some_variable_2 = incorrectfb.Value;
于 2013-09-26T19:08:31.857 に答える