Link to home
Create AccountLog in
Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

asked on

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_REPORT>
- <ERRORS STATUS="TRUE">
  <ERROR CODE="" DESC="" />
  </ERRORS>
  <ERROR_DETAIL FIELD_NO="" ERROR_MESSAGE="" FIELD_NAME="" />
</ ERROR_REPORT >
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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("Exists");
            else
                Console.WriteLine("Does not exist");

Open in new window

Avatar of RadhaKrishnaKiJaya
RadhaKrishnaKiJaya

ASKER

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
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thank you Chris Stanyon and zc2 for your time and help.  It worked like charm.