I am trying to search an xml element from the root element of the file on the basis of inner text. i have tried this but didnt work:
rootElRecDocXml.SelectSingleNode("/ArrayOfRecentFiles[name='"+mFilePath+"']");
I know the old school way to traverse all file element by element but i dont want to do that.
Please note that: my root element name is ArrayOfRecentFiles
and my child element name are RecentFile
We'd need to see the xml; @Lee gives the correct approach here, so something like:
var el = rootElRecDocXml.SelectSingleNode(
"/ArrayOfRecentFiles/RecentFile[text()='"+mFilePath+"']");
(taking your edit/reply into account)
However! There are lots of gotchas:
<foo>abc</foo>
is different to <foo> abc[newline]</foo>
etc - ditto carriage return).SelectSingleNode("/alias:ArrayOfRecentFiles[text()='"+mFilePath+"']", nsmgr);
, where nsmgr
is the namespace-managerTo give a complete example, that matches your comment:
XmlDocument rootElRecDocXml = new XmlDocument();
rootElRecDocXml.LoadXml(@"<ArrayOfRecentFiles> <RecentFile>C:\asd\1\Examples\8389.atc</RecentFile> <RecentFile>C:\asd\1\Examples\8385.atc</RecentFile> </ArrayOfRecentFiles>");
string mFilePath = @"C:\asd\1\Examples\8385.atc";
var el = rootElRecDocXml.SelectSingleNode(
"/ArrayOfRecentFiles/RecentFile[text()='" + mFilePath + "']");
Here, el
is not null
after the SelectSingleNode
call. It finds the node.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With