Find an element in xml by its inner text

I am trying to search for an xml element from the root element of a file based on inner text. I tried this but did not work:

rootElRecDocXml.SelectSingleNode("/ArrayOfRecentFiles[name='"+mFilePath+"']");

I know that the old school way to go through the whole element of a file by element, but I do not want to do this.

Please note that: the name of my root element ArrayOfRecentFilesand the name of my childRecentFile

+5
source share
2 answers

We need to see xml; @Lee gives the correct approach here, so something like:

var el = rootElRecDocXml.SelectSingleNode(
          "/ArrayOfRecentFiles/RecentFile[text()='"+mFilePath+"']");

(taking into account your answer / answer)

However! There are many mistakes:

  • the request will be case sensitive
  • ( <foo>abc</foo> <foo> abc[newline]</foo> .. - )
  • xml , .SelectSingleNode("/alias:ArrayOfRecentFiles[text()='"+mFilePath+"']", nsmgr);, nsmgr -

, :

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 + "']");

el null SelectSingleNode. node.

+5

"text()" , .

rootElRecDocXml.SelectSingleNode("/ArrayOfRecentFiles[text()='"+mFilePath+"']");
+3

All Articles