循环访问同名的元素,并按另一个属性值返回属性
本文关键字:属性 另一个 返回 访问 元素 循环 | 更新日期: 2023-09-27 17:56:55
我正在尝试使用 LINQ 查找具有特定属性值的特定元素并返回另一个属性值。如果我将要查找的元素的名称更改为唯一名称并以这种方式搜索它,它将返回正确的值。不幸的是,我无法实时执行此操作,因为我无法控制XML格式。那么,如何让 LINQ 遍历每个元素"行"并返回我正在寻找的属性。
编辑:错误是空引用
XML(示例):
<currentTime>2013-01-31 12:34:49</currentTime>
<result>
<rowset name="kills" key="killID" columns="killID,solarSystemID,killTime,moonID">
<row killID="28024540" solarSystemID="30000967" killTime="2013-01-25 21:41:00" moonID="0">
<victim characterID="888493667" characterName="Syrsyrian" corporationID="98152004" corporationName="Eviscerate." allianceID="0" allianceName="" factionID="0" factionName="" damageTaken="20220" shipTypeID="4306" />
<rowset name="attackers" columns="characterID,characterName,corporationID,corporationName,allianceID,allianceName,factionID,factionName,securityStatus,damageDone,finalBlow,weaponTypeID,shipTypeID">
<row characterID="1062593035" characterName="cal dasiri" corporationID="310281086" corporationName="Un4seen Development" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="5.00586641950613" damageDone="2286" finalBlow="1" weaponTypeID="2629" shipTypeID="24698" />
<row characterID="0" characterName="" corporationID="98074388" corporationName="The New Lunar Republic" allianceID="0" allianceName="" factionID="0" factionName="" securityStatus="0" damageDone="9620" finalBlow="0" weaponTypeID="0" shipTypeID="17404" />
<row characterID="584494552" characterName="Diesel24" corporationID="753753190" corporationName="Space-Bar" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="3.5" damageDone="2322" finalBlow="0" weaponTypeID="24702" shipTypeID="24702" />
<row characterID="90905050" characterName="Thomas Shaefer" corporationID="187930037" corporationName="The Partisan Brigade" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="4.1" damageDone="1940" finalBlow="0" weaponTypeID="27315" shipTypeID="16227" />
<row characterID="624484955" characterName="Full Penetration" corporationID="187930037" corporationName="The Partisan Brigade" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="3.1" damageDone="1788" finalBlow="0" weaponTypeID="27371" shipTypeID="16227" />
<row characterID="1695597027" characterName="Algazi" corporationID="187930037" corporationName="The Partisan Brigade" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="1.5" damageDone="1258" finalBlow="0" weaponTypeID="1818" shipTypeID="24698" />
<row characterID="92087835" characterName="Yasin Stavrouski" corporationID="310281086" corporationName="Un4seen Development" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="0.4" damageDone="561" finalBlow="0" weaponTypeID="8819" shipTypeID="587" />
<row characterID="581357145" characterName="Nykitah" corporationID="187930037" corporationName="The Partisan Brigade" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="5" damageDone="445" finalBlow="0" weaponTypeID="24702" shipTypeID="24702" />
<row characterID="92692957" characterName="Mokey PYD" corporationID="98153124" corporationName="Plots Your Demise" allianceID="0" allianceName="" factionID="0" factionName="" securityStatus="0" damageDone="0" finalBlow="0" weaponTypeID="5399" shipTypeID="594" />
<row characterID="90511370" characterName="Rae'll" corporationID="187930037" corporationName="The Partisan Brigade" allianceID="1566707582" allianceName="Republic Alliance" factionID="0" factionName="" securityStatus="-0.4" damageDone="0" finalBlow="0" weaponTypeID="527" shipTypeID="670" />
</rowset>
<rowset name="items" columns="typeID,flag,qtyDropped,qtyDestroyed,singleton">
<row typeID="3090" flag="31" qtyDropped="0" qtyDestroyed="1" singleton="0" />
<row typeID="31564" flag="94" qtyDropped="0" qtyDestroyed="1" singleton="0" />
和查询:
XAttribute two = (from kd1 in loadkillData1.Elements("row")
where (string)kd1.Attribute("characterName") == "cal dasiri"
select kd1.Attribute("allianceName")).FirstOrDefault();
string derp2 = two.Value;
label10.Content = derp2;
要获取 row
的所有实例,请使用 xd.Descendants()
XAttribute two = (from kd1 in xd.Descendants("row")
where (string)kd1.Attribute("characterName") == "cal dasiri"
select kd1.Attribute("allianceName")).FirstOrDefault();