Hi,
I have thousand of records in Xml file. I want to filter data just like as sql server "Like" operator. Is it possible on Xml File or not?
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jaganathan BantheswaranPosted Sep 30, 2013, 5:09 AM
using System.IO;
using System;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
class Program
{
static void Main()
{
string xml = @"
using (StringReader stream = new StringReader(xml))
{
XPathDocument doc = new XPathDocument(stream);
XPathNavigator nav = doc.CreateNavigator();
// Compile a standard XPath expression
XPathExpression expr;
//If your Env is XPath 2.0 then use lower-case() instead of translate
expr = nav.Compile("/root/customer/lastname[contains(translate(text(),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 'SMITH')] | /root/customer/firstname[contains(translate(text(),'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 'SMITH')] ");
XPathNodeIterator iterator = nav.Select(expr);
try
{
while (iterator.MoveNext())
{
XPathNavigator nav2 = iterator.Current.Clone();
Console.WriteLine("Found: " + nav2.Value);
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Sunny SharmaPosted Sep 30, 2013, 4:34 AM
VulpesPosted Sep 30, 2013, 4:22 AM
I've added a root node to the file which now looks like this:
As expected, the output of the program is:
Gurjeet SinghPosted Sep 30, 2013, 3:56 AM
Sunny SharmaPosted Sep 30, 2013, 3:36 AM
please show me XMlL (sample) structure and what you want to do.