【发布时间】:2016-05-26 19:43:22
【问题描述】:
我有一个 Linq to Xml 查询,它需要根据特定节点的属性值检索一个值。我正在尝试检索项目列表,其中一个节点具有我似乎无法找到获取值的方法的属性。 这是 XML:
<codelist-items>
<codelist-item>
<code>1</code>
<name>
<narrative>Planned start</narrative>
<narrative xml:lang="fr">Début prévu</narrative>
</name>
<description>
<narrative>
The date on which the activity is planned to start, for example the date of the first planned disbursement or when physical activity starts.
</narrative>
</description>
</codelist-item>
<codelist-item>
<code>2</code>
<name>
<narrative>Actual start</narrative>
<narrative xml:lang="fr">Début réel</narrative>
</name>
<description>
<narrative>
The actual date the activity starts, for example the date of the first disbursement or when physical activity starts.
</narrative>
</description>
</codelist-item>
</codelist-items>
为了简短起见,我只显示 2 个项目。这是我的 Linq 查询,试图从“name/narrative”中检索值,其中有一个“xml:lang='fr'”属性:
XElement xelement = XElement.Load(xmlFile);
var elements = from adt in xelement.Elements("codelist-items").Elements("codelist-item")
select new ActivityDateType
{
Code = (string)adt.Element("code"),
NameEng = (string)adt.Element("name").Element("narrative"),
NameFra = (string)adt.Element("name").Element("narrative[@xml:lang='fr']"),
Description = (string)adt.Element("description")
};
return elements;
有人知道如何获取 NameFra 的值吗?
谢谢
【问题讨论】:
标签: c# xml xpath asp.net-web-api linq-to-xml