I'm stumped (again) with an misunderstanding of XDocument/Linq. For the XML below, I have nameEn
and provinceCode
as variables in my code. I'm trying to identify the code
(e.g., s0000002
) and nameFr
given I have the other two elements. The provinceCode
and NameEn
combined are unique in the XML (no duplication).
<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