简体   繁体   English

C#中的初学者XPath表达式错误

[英]beginner XPath expression error in C#

I am trying to use following XPath expressions: 我试图使用以下XPath表达式:

nodeList = root.SelectNodes("/moviedb/movie[genres="Thriller"]");
nodeList = root.SelectNodes("/moviedb/movie[contains(genres, "+ ElementValue +")]");

where ElementValue is user input value 其中ElementValue是用户输入值

For the first line i get errors like: 对于第一行我得到的错误如下:

Error 4 ) expected 错误4)预期
Error 6 Invalid expression term ')' 错误6无效的表达式术语')'

While second expression returns 0 而第二个表达式返回0

Before i used this expressions in the c#, i did test them online and they worked. 在我在c#中使用这个表达式之前,我确实在线测试了它们并且它们有效。 My xml looks something like this: 我的xml看起来像这样:

<moviedb>
<movie>
    <imdbid>tt2226321</imdbid>
    <genres>Thriller</genres>
    <languages>English</languages>
    <country>USA</country>
    <rating>8</rating>
    <runtime>155</runtime>
    <title>The Dark Knight</title>
    <year>2014</year>
  </movie>
  <movie>
    <imdbid>tt1959490</imdbid>
    <genres>Action,Adventure,Drama</genres>
    <languages>English</languages>                                        
    <country>USA</country>
    <rating>6.5</rating>
    <runtime>138</runtime>
    <title>Noah</title>
    <year>2014</year>
  </movie>
</moviedb>

Thanks 谢谢

When dealing with quotes in strings you need to take special steps, as inserting any quotes will make the compiler think you are ending or starting a new string. 在处理字符串中的引号时,您需要采取特殊步骤,因为插入任何引号将使编译器认为您正在结束或启动新字符串。

Either use the \\" escape sequence to show the compiler that you mean to insert a quote mark instead of the end of a string, or the @ symbol and use 2 double quotes ( "" ) 使用\\"转义序列向编译器显示您要插入引号而不是字符串的结尾,或@符号并使用2个双引号( ""

nodeList = root.SelectNodes(@"/moviedb/movie[genres=""Thriller""]");

or 要么

nodeList = root.SelectNodes("/moviedb/movie[genres=\"Thriller\"]");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM