I mumped (again) with an 误解 of XDocument/Linq. 关于下文的XML,我有<代码>名称En和-provinceCode
,作为我的代码中的变量。 我试图确定<代码>代码代码>(例如,,因为我有另外两个要素。 The
provinceCode
and NameEn
joint are separate in the XML (noplica).
<siteList>
<site code="s0000001">
<nameEn>Edmonton</nameEn>
<nameFr>Edmonton</nameFr>
<provinceCode>AB</provinceCode>
</site>
<site code="s0000002">
<nameEn>Algonquin Park</nameEn>
<nameFr>Parc Algonquin</nameFr>
<provinceCode>ON</provinceCode>
</site>
...
</siteList>
Here s the code I m trying (my XML is in the "loaded" XDocument:
selectedProvince = "ON";
selectedCity = "Algonquin Park";
strSiteCode = loaded.Descendants("site")
.Where(x => x.Element("provinceCode").Value == selectedProvince)
.Where(x => x.Element("nameEn").Value == selectedCity)
.Select(x => x.Element("code").Value)
.ToString();
strNameFR = loaded.Descendants("site")
.Where(x => x.Element("provinceCode").Value == selectedProvince)
.Where (x => x.Element("nameEn").Value == selectedCity)
.Select(x => x.Element("nameFr").Value)
.ToString();
The string strSiteCode
returns: System.Linq.Enumerable+WhereSelectEnumerableIterator
2[System.Xml.Linq.XElement,System.String]and
strNameFRreturns
""`.
I can t figure out what the working code should look like. Thanks for any help.
Doug