我想用xml源文件構建對象。 我需要獲得與特定「SiteName」匹配的「網站」元素。 然後用'Site'元素中的所有元素構建我的對象(LocalisationInfo類)。 謝謝。LinqToXml使用xml文件構造類對象
這裏是XML文件的樣子:
<Sites>
<Site>
<longitude>1.555</longitude>
<latitude>4.488003</latitude>
<SiteName>Aix en Provence</SiteName>
<FullAdresse>adresse here</FullAdresse>
<NomResponsable>martin Véronique</NomResponsable>
<AdressePostale>260 rue Louis de Broglie</AdressePostale>
<CodePostal>13799</CodePostal>
<Ville>Aix-En-Provence</Ville>
<Pays>France</Pays>
</Site>
<Site>
</Site>
<Site>
</Site>
這裏是我的代碼是不工作...
XDocument xdoc = XDocument.Load("Resources/Sites.xml");
var lesite = from d in xdoc.Descendants("Site")
where d.Element("SiteName").Value == sitelocalisation
select new LocalisationInfo
{
TelSecouriste = d.Element("TelSecouriste").Value,
Longitude = d.Element("longitude").Value,
Latitude = d.Element("latitude").Value,
Pays = d.Element("Pays").Value,
CodePostal = d.Element("CodePostal").Value,
Ville = d.Element("Ville").Value,
Standard = d.Element("Standard").Value,
SiteName = d.Element("SiteName").Value,
AdressePostale = d.Element("AdressePostale").Value,
};
究竟是什麼不工作?你有例外嗎? –