I have the following C# code snippet to use XPath to find errors in a XML file which is being input:
string xml; // the XML is passed as a parameter as the string below
using (Stream messageStream = new MemoryStream(xml))
{
IXPathNavigable source = new XPathDocument(messageStream);
XPathNavigator navigator = source.CreateNavigator();
object evaResult = navigator.Evaluate("boolean(/thinktransferDataSet/ErrorLog)"); // returns false
XPathNodeIterator iterator = navigator.Select("/thinktransferDataSet/ErrorLog/sKey"); // returns empty iterator
// Assert evaResult is true and iterator has elements. Both assertions fail
}
Here is the text of the XML file:
<?xml version="1.0" standalone="yes"?>
<thinktransferDataSet xmlns="http://tempuri.org/thinktransferDataSet1.xsd">
<ProcessHeader>
<sKey>uniqueId</sKey>
<sApplication>appname</sApplication>
<sUser>username</sUser>
<dtProcessDate>Oct 8 2015 9:58AM</dtProcessDate>
<iProcessId>5132</iProcessId>
<iTranFailureCount>2</iTranFailureCount>
<iTranSuccessCount>0</iTranSuccessCount>
</ProcessHeader>
<ErrorLog>
<sKey>uniqueId</sKey>
<sLevel>ERROR</sLevel>
<sDescription>Error in table :tablename Row:1 - Column 'comments' exceeds the MaxLength limit.</sDescription>
<sSource>69</sSource>
<dtDate>Oct 8 2015 9:59AM</dtDate>
</ErrorLog>
<ErrorLog>
<sKey>uniqueId</sKey>
<sLevel>ERROR</sLevel>
<sDescription>Test error description</sDescription>
<sSource>69</sSource>
<dtDate>Oct 8 2015 9:59AM</dtDate>
</ErrorLog>
</thinktransferDataSet>
Errors are indicated by the presence of one or more elements in the XML. However, with the test message below the navigator.Evaluate and navigator.Select elements are simply not finding the elements or ErrorLog/sKey elements even though these XPath strings completely work in XMLSpy. What could be the issue here?
Many thanks!