Monday, 5 June 2017

Read XPath value from XML file

Reading xpath value from XML file 

As stated above  its easy way to read the xpath query using XMLreader and XPATHDocument;
Below is the sample code 

using System.Xml;
using System.Xml.XPath;

private string ReadXpath(string xmlFilePath , string xPath)
            string xpathValue="";

            XmlReader reader = XmlReader.Create(xmlFilePath);

               if (reader.Read())
                    XPathDocument xPathDoc = new XPathDocument(reader);
                    XPathNavigator xNavigator = xPathDoc.CreateNavigator();
                    XPathNodeIterator xNodes = xNavigator.Select(xPath);

                    if (xNodes.Count != 0 && xNodes.MoveNext())
                        xpathValue = xNodes.Current.Value;

            return xpathValue;