如果當前的「名稱」元素爲空或爲空,則應該使用前一個元素的「名稱」值。第一個元素名稱永遠不會爲空或空白。自己獲取元素
我需要得到由先前的元素「名稱」值在當前「名稱」元素的值是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>
'System.Xml.Linq.XNode'沒有包含'Element'的定義,也沒有找到接受'System.Xml.Linq.XNode'類型的第一個參數的擴展方法'Element' – BossRoss
對不起,我忘了將PreviousNode投射到XElement,我已經更新了答案 –
無法投射System.Xml.Linq.XText類型的對象來鍵入System.Xml.Linq.XElement – BossRoss