Check a Node in XML Doc in C#

Hello Experts,
I have an XML Document named ErrorRowsXml as below.  I need to check the existence of the node ERROR_DETAIL in C#.  How can I do that.

Thank you very much in advance.

  <ERROR CODE="" DESC="" />
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Here's a quick and basic example:

void CheckXmlFile(string xmlFile)
    // Create an XmlDocument
    XmlDocument xmlDoc = new XmlDocument();

    // Load in your file

    // Select the ERROR_DETAIL node
    XmlNode errorDetail = xmlDoc.DocumentElement.SelectSingleNode("ERROR_DETAIL");

    // Check if the node exists
    if (errorDetail != null)
        // Loop through the node's attributes
        foreach (XmlAttribute att in errorDetail.Attributes)
            Console.WriteLine($"Name {att.Name} | Value {att.Value}");
        // There was no ERROR_DETAIL node
        MessageBox.Show("No ERROR_DETAIL node!");

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The easiest way is to use the SelectSingleNode() method:
            XmlDocument xml = new XmlDocument();
            xml.Load( "ErrorRowsXml.xml" );
            if( xml.SelectSingleNode( "/ERROR_REPORT/ERROR_DETAIL" ) != null ) 
                Console.WriteLine("Does not exist");

Open in new window

RadhaKrishnaKiJayaAuthor Commented:
Chris Stanyon and zc2,
Thank you for your time and help.  

I am able to check it.  Now, once checked, if null I need to add the node.  How can I do that?

Thank you
Could by done like the following:
            else {
                Console.WriteLine("Does not exist");
                XmlElement ed = xml.CreateElement("ERROR_DETAIL");
                ed.SetAttribute( "FIELD_NO", "" );
                ed.SetAttribute( "ERROR_MESSAGE", "" );
                ed.SetAttribute( "FIELD_NAME", "" );
                xml.DocumentElement.AppendChild( ed );

Open in new window

RadhaKrishnaKiJayaAuthor Commented:
Thank you Chris Stanyon and zc2 for your time and help.  It worked like charm.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.