我有以下需要使用 linq 解析和获取值的 xml
<?xml version="1.0" encoding="UTF-8"?>
<questestinterop xmlns="http://www.imsglobal....." xmlns:xsi="http://www.w3.org...." xsi:schemaLocation="http://www.imsglobal.org/.....xsd">
<assessment ident="gh" title="Sample">
<qtimetadata>
<qtimetadatafield>
<fieldlabel>cc_maxattempts</fieldlabel>
<fieldentry>1</fieldentry>
</qtimetadatafield>
</qtimetadata>
<section ident="rs">
<item ident="ghg" title="Question">
<itemmetadata>
<qtimetadata>
<qtimetadatafield>
<fieldlabel>question_type</fieldlabel>
<fieldentry>yyy</fieldentry>
</qtimetadatafield>
<qtimetadatafield>
<fieldlabel>points_possible</fieldlabel>
<fieldentry>1</fieldentry>
</qtimetadatafield>
<qtimetadatafield>
<itemmetadata>
<presentation>
.........
</presentation>
</item>
<item ident="hj" title="Question">
....
</item>
</section>
</assessment>
</questestinterop>
我需要获取 yyy where fieldlabel = questiontype 的值。我如何使用 linq 做到这一点?尝试了以下方法:
XDocument document = XDocument.Load(assessmentXmlName);
XNamespace ns = "http://www.imsglobal.org/xsd/ims_qtiasiv1p2";
var results = from main in document.Root
.Elements(ns + "assessment")
.Elements(ns + "section")
.Elements(ns + "item")
.Elements(ns + "itemmetadata")
.Elements(ns + "qtimetadata")
.Descendants("qtimetadatafield")
select new
{
itemtype = (string)main.Element(ns + "fieldentry").Value,
};
让我知道