如果当前“名称”元素为空或为空,则它应该采用前一个元素的“名称”值。第一个元素名称永远不会为 null 或为空。
我需要获取当前“名称”元素值为空或为空的先前元素“名称”值:
List<ScripTbl> ScripData = ScrpHldNode.Descendants(ns + "Scrp").Select(x => new ScripTbl
{
ID = sID,
//need help with the line below....
ScripShare = (!string.IsNullOrEmpty((String)x.Element(ns + "Name")) ? (String)x.Element(ns + "Name") : (x.ElementsBeforeSelf().Count() > 0 ? (String)x.ElementsBeforeSelf().Last().Element(ns + "Name") : "")),
ScripOpenBalanceDue = (String)x.Element(ns + "OpBal"),
ScripClosingBalance = (String)x.Element(ns + "ClBal"),
ScripTransaction = (String)x.Element(ns + "Tran"),
ScripMovement = (String)x.Element(ns + "Move"),
ScripFooter = sScripFooter,
Seq = x.ElementsBeforeSelf().Count() + 1,
ScripSectorCode = (String)x.Element(ns + "SecCde"),
ScripClosingPrice = (String)x.Element(ns + "ClPrce"),
SplitIndicator = (!string.IsNullOrEmpty((String)x.Element(ns + "Name")) ? (x.ElementsBeforeSelf().Count() > 0 && (String)x.Element(ns + "Name") == (String)x.ElementsBeforeSelf().Last().Element(ns + "Name") ? "1" : "0") : "0")
}).ToList();
和 XML:
<ScrpHld xmlns="http://www.website.co.za/namespace">
<Scrp>
<Name>Company Name Number 1</Name>
<SecCde>1366J</SecCde>
<Tran>SOLD</Tran>
<OpBal>0</OpBal>
<Move>-2000</Move>
</Scrp>
<Scrp>
<Tran>ELECTRONIC SETTLEMENT</Tran>
<Move>2000</Move>
<ClPrce>25045.00</ClPrce>
<ClBal>0</ClBal>
</Scrp>
<Scrp>
<Name>Company Name Number 2</Name>
<SecCde>1313J</SecCde>
<Tran>SOLD</Tran>
<OpBal>10000</OpBal>
<Move>-90500</Move>
</Scrp>
<Scrp>
<Tran>ELECTRONIC SETTLEMENT</Tran>
<Move>80500</Move>
<ClPrce>3392.00</ClPrce>
<ClBal>0</ClBal>
</Scrp>
</ScrpHld>