我的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" 标识。这是我尝试过的。假设 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;
}