Select Element by xpath - CSharp System.Xml

CSharp examples for System.Xml:XML Element

Description

Select Element by xpath

Demo Code


using System.Xml.Linq;
using System.Threading.Tasks;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*ww  w .  java  2 s  .  c o  m*/

public class Main{
        public static XElement SelectElement(XElement doc, string path)
        {
            XElement res = null;
            if (string.IsNullOrEmpty(path))
            {
                return res;
            }
            var tracks = path.Split('/');
            res = doc.Element(tracks[0]);
            for (int i = 1; (i < tracks.Length) && (res != null); i++)
            {
                res = res.Element(tracks[i]);
            }
            return res;
        }
}

Related Tutorials