Like statement in Xpath

xpath , Csharp.net , xml India
  • 11 years ago

    using System; using System.Xml;

    namespace Project1 { class Class { [STAThread] static void Main(string[] args) {

     XmlDocument xmlDoc; 
     XmlNode     xmlNode; 
     String      xpathExpr; 
    
     xmlDoc = new XmlDocument(); 
     xmlDoc.LoadXml( "<?xml version='1.0'?><root><person1><name>Martin Fowler</name></person1><person2><name>Kent Beck</name></person2><person3><name>Dave Astels</name></person3><xyz></xyz><abc></abc></root>" ); 
    
     xpathExpr = "/root/person[name='Martin Fowler']"; 
     xmlNode = xmlDoc.SelectSingleNode(xpathExpr); 
    
     Console.WriteLine(xmlNode.OuterXml); 
     Console.ReadLine()
    

    } } }

    in the above example, can I get the all person() nodes.Its nothing but person% concept using xpath or any other thing. Please help me.

  • 11 years ago

    using System; using System.Xml;

    namespace Project1 { class Class { [STAThread] static void Main(string[] args) {

     XmlDocument xmlDoc; 
     XmlNode     xmlNode; 
     String      xpathExpr; 
    
     xmlDoc = new XmlDocument(); 
     xmlDoc.LoadXml( "<?xml version='1.0'?><root><person1><name>Martin Fowler</name></person1><person2><name>Kent Beck</name></person2><person3><name>Dave Astels</name></person3><xyz></xyz><abc></abc></root>" ); 
    
     xpathExpr = "/root/person[name='Martin Fowler']"; 
     xmlNode = xmlDoc.SelectSingleNode(xpathExpr); 
    
     Console.WriteLine(xmlNode.OuterXml); 
     Console.ReadLine()
    

    } } }

    in the above example, can I get the all person() nodes.Its nothing but person% concept using xpath or any other thing. Please help me.

  • 11 years ago

    Hi,

    you can use the following statement: "/root/person[contains(name,'Martin Fowler')]"

    You can get all the person nodes from the above statement.

    Cheers, Niroshan

  • 11 years ago

    Hi,

    Thanks for the reply. But it gives the nodes which contains name as 'Martin Fowler'. But what i required is all person nodes(person1,person2,person 3..) out of the total xml. example abcnde ererere abcnde ererere

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems.” - Jamie Zawinski