在 C# 中使用 XPath 的条件表达式
Conditional expressions in C# using XPath
我必须使用输入 xml 中可用的数据创建一个实体对象。该对象的其中一个属性的值取决于条件,在 XPath 中看起来像这样:
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']) then 'Y' else 'N'' 有一个无效的令牌。
所以我的问题是:
- C#/.Net (4.5) 是否支持此条件表达式?
- 如果不是,当我们必须在 XPath 中检查多个条件时,推荐的方法是什么?
谢谢
迪莱普
你可以这样写 XPath:
<xsl:choose>
<xsl:when test="//trade/tradeHeader/tradeTypology/tradeCategoryTypology[@tradeCategory ='TechnicalCancellation']">
<xsl:value-of select="'Y'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'N'"/>
</xsl:otherwise>
</xsl:choose>
您的 XSL 代码中可以有很多 <xsl:when>
条件。
XPath 1.0 没有条件(而 vanilla .NET 仅支持 XPath 1.0)。
但是,当您实际上可以使用宿主语言时,我看不出在 XPath 中选择 "Y"
或 "N"
有什么意义,所以
有什么问题
private static string GetValueFromXml(XmlDocument xDoc, string xPath)
{
var node = xDoc.SelectSingleNode(xPath);
return (node != null) node.InnerText : null;
}
private static void Test()
{
var path = "//trade/tradeHeader/tradeTypology/tradeCategoryTypology[tradeCategory = 'TechnicalCancellation']";
var doc = GetYourXmlDocumentSomehow();
var result = GetValueFromXml(doc, path) == null ? "N" : "Y";
}
?
如果您绝对肯定必须使用 XPath,您可以使用
substring(
'NY',
count(
//trade/tradeHeader/tradeTypology/tradeCategoryTypology[tradeCategory = 'TechnicalCancellation'][1]
) + 1,
1
)
这是我的答案的变体 in the thread @xanatos mentions in the comments。
我必须使用输入 xml 中可用的数据创建一个实体对象。该对象的其中一个属性的值取决于条件,在 XPath 中看起来像这样:
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']) then 'Y' else 'N'' 有一个无效的令牌。
所以我的问题是:
- C#/.Net (4.5) 是否支持此条件表达式?
- 如果不是,当我们必须在 XPath 中检查多个条件时,推荐的方法是什么?
谢谢 迪莱普
你可以这样写 XPath:
<xsl:choose>
<xsl:when test="//trade/tradeHeader/tradeTypology/tradeCategoryTypology[@tradeCategory ='TechnicalCancellation']">
<xsl:value-of select="'Y'"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="'N'"/>
</xsl:otherwise>
</xsl:choose>
您的 XSL 代码中可以有很多 <xsl:when>
条件。
XPath 1.0 没有条件(而 vanilla .NET 仅支持 XPath 1.0)。
但是,当您实际上可以使用宿主语言时,我看不出在 XPath 中选择 "Y"
或 "N"
有什么意义,所以
private static string GetValueFromXml(XmlDocument xDoc, string xPath)
{
var node = xDoc.SelectSingleNode(xPath);
return (node != null) node.InnerText : null;
}
private static void Test()
{
var path = "//trade/tradeHeader/tradeTypology/tradeCategoryTypology[tradeCategory = 'TechnicalCancellation']";
var doc = GetYourXmlDocumentSomehow();
var result = GetValueFromXml(doc, path) == null ? "N" : "Y";
}
?
如果您绝对肯定必须使用 XPath,您可以使用
substring(
'NY',
count(
//trade/tradeHeader/tradeTypology/tradeCategoryTypology[tradeCategory = 'TechnicalCancellation'][1]
) + 1,
1
)
这是我的答案的变体 in the thread @xanatos mentions in the comments。