Entwickler-Ecke

Datenbanken (inkl. ADO.NET) - Linq to XML / mehrere Attribute eines Elements auslesen


zappo - Mi 14.10.09 14:14
Titel: Linq to XML / mehrere Attribute eines Elements auslesen
Ich habe folgendes Problem, von unten gezeigter XML Datei möchte ich alle Attribute eines Elements auslesen wenn das Attribut "type" eine bestimmte Zeichenfolge enthält.


XML-Daten
1:
2:
3:
4:
5:
6:
7:
<?xml version="1.0" encoding="utf-8"?>
<Module_Data_Delivered Version="1.00" Country_Version="Deutsch" Date="06.10.2009">
  <Modules>
    <Module type="xxx" article_no="123456" distributor="xx" nominal_power="200" tolerance_plus="5" tolerance_minus="0" guaranteed_min_power="200" nominal_voltage="26,70" nominal_current="7,12" open_circuit_voltage="32,60" short_ciruit_current="7,98" efficiency_factor_cells="13,4" module_efficiency="13,4" temp_coeff_pmpp="-0,4" temp_coeff_isc="0,04" temp_coeff_uoc="-0,38" temp_coeff_impp="0,04" temp_coeff_umpp="-0,38" noct="45" max_system_voltage="1000" cells_count="" length="" width="" height="" weight="" frame_color="" frame_type="" frontglas_type="" frontglas_thickness="" connection_type="" length_cables="" inverter_wo_tr="" warning_countries="" module_technology="" module_technology_efficiency_factor="" />
    <Module type="xxx2" article_no="123457" distributor="x" nominal_power="165" tolerance_plus="5" tolerance_minus="0" guaranteed_min_power="165" nominal_voltage="35,8" nominal_current="4,61" open_circuit_voltage="44" short_ciruit_current="5,1" efficiency_factor_cells="16" module_efficiency="12,9" temp_coeff_pmpp="-0,4" temp_coeff_isc="0,04" temp_coeff_uoc="-0,38" temp_coeff_impp="0,04" temp_coeff_umpp="-0,38" noct="45" max_system_voltage="1000" cells_count="" length="" width="" height="" weight="" frame_color="" frame_type="" frontglas_type="" frontglas_thickness="" connection_type="" length_cables="" inverter_wo_tr="" warning_countries="" module_technology="" module_technology_efficiency_factor="" />
  </Modules>
</Module_Data_Delivered>


Nun habe ich das ganze schon via Linq versucht,


C#-Quelltext
1:
2:
3:
var query = from c in file.Elements("Modules").Elements("Module").Attributes()
where c.Value == "xxx2"
select c;

naja.. das ist es offensicht noch nicht.. kann mir jemand auf die Sprünge helfen? Vielleicht auch nur mit einem guten Link zu LinQ?

Gruß


zappo - Mi 14.10.09 14:23

Aaah...

Die Lösung liegt im Rückgabetyp Enumberable.
bzw. das erlangen der Daten dann in der foreach

-->

C#-Quelltext
1:
2:
foreach(var c in query)
Console.WriteLine(c.Attribute("nominal_power").Value + c.Attribute("....").Value + .... )


Das bringts..
Gruß