我必須使用輸入xml中可用的數據創建實體對象。此對象的屬性之一的值取決於一個條件,它看起來像這樣的XPath:使用XPath的C#中的條件表達式
if (//trade/tradeHeader/tradeTypology/tradeCategoryTypology[tradeCategory = 'TechnicalCancellation']) then 'Y' else 'N'")
及以下函數接受這個XPath和XML文檔:
private static string GetValueFromXml(XmlDocument xDoc, string xPath)
{
var nod = xDoc.SelectSingleNode(xPath);
if (nod != null)
return nod.InnerText;
return null;
}
然而,它不起作用。錯誤是:
'if(// trade/tradeHeader/tradeTypology/tradeCategoryTypology [tradeCategory ='TechnicalCancellation'])那麼'Y'else'N''有一個無效的標記。
所以我的問題是:
- 是在C#/支持這一條件表達式網(4.5)?
- 如果不是,當我們必須檢查XPath中的多個條件時,推薦的方法是什麼?
感謝 迪利普
在XPath 1.0中沒有'if',.NET僅支持XPath 1.0。請參閱http://stackoverflow.com/questions/971067/is-there-an-if-then-else-statement-in-xpath?rq=1瞭解可能的解決方案 – xanatos 2015-04-06 13:31:07