How to search Single node from XML

The following code sample shows how to search Single node from XML

XmlTextReader reader;//For read xml file

XmlDocument doc = new XmlDocument();

reader = new XmlTextReader("XMLFile\\testing.xml");

//load xml file in xml document object


//close xml reader


//Read all nodes in xml file

XmlNodeList newXMLNodes = doc.SelectNodes("/pip/error");

//check for deleting data

foreach (XmlNode newXMLNode in newXMLNodes)

ring code = newXMLNode.SelectSingleNode("error_code").InnerText;

string message = newXMLNode.SelectSingleNode("error_message").InnerText;



Author: Kapil Dhawan18 Jun 2008 Member Level: Gold   Points : 2

Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you

Author: Shivshanker Cheral18 Jun 2008 Member Level: Gold   Points : 0

Thanks for sharing

Author: srirama11 Feb 2014 Member Level: Gold   Points : 2

You can do in this way also....

Xmldocument xmldoc =new XmlDocument();
xmldoc.Load(pathof the filename);
var curnode = xmldoc.selectSingleNode("//pop/error");
string nodename = currnode.Text

The difference between two is SelectSingleNode will return fairst matching node rather than the list of all matching Nodes

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: