简体   繁体   中英

Search string in cdata block

One of the columns of my Microsoft SQL Server table contains a string formatted as XML

<root>
    <de><![CDATA[something]]></de>
    <fr><![CDATA[something]]></fr>
    <it><![CDATA[something]]></it>
    <en><![CDATA[something]]></en>
</root>

or

<root>
    <de>something</de>
    <it>something</it>
    <fr>something</fr>
    <en>something</en>
</root>

I need to extract all the records that contains a certain word, no matter if there's the CDATA or not.

So I structured the where conditions in LINQ in this way:

vc.Url.Contains(">" + URL + "<") || vc.Url.Contains("cdata[[]" + URL + "]")

If the searchtext matches and there is not CDATA, then the where condition works and I get the result.

if the searchtext matches and there is the CDATA, then the where condition doesn't work.

If I execute the query via SQL Server Management Studio:

  where vc.Url like '%>something<%' OR vc.Url like '%cdata[[]something]%'

I obtain the expected result.

Any idea about how to write the where condition in the right way and obtain the same result via SQL and via LINQ?

Thanks in advance!

Entity Framework automatically escapes any special characters passed in to Contains , StartsWith , and EndsWith , so you don't need to use [[] . Try:

vc.Url.Contains(">" + URL + "<") || vc.Url.Contains("cdata[" + URL + "]")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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