Read First Node from XMLDocument

Ocean picture Ocean · Feb 8, 2011 · Viewed 20.2k times · Source

I receive message in XML string; that I load into XmlDocument; but second node is different every time; I have given example below are three examples:

 <Message> 
    <Event1 Operation="Amended" Id="88888">Other XML Text</Event1>
 </Message>
 <Message>
    <Event2 _Operation_="Cancelled" Id="9999999"> Other XML Text </Event2>
 </Message> 
 <Message> 
    <Event3 Operation="Cancelled" Id="22222"> Other XML Text </Event3>
 </Message>

Now, I want to find out whether second node is Event1 or Event2 or Event3 and also what is value of Operation e.g. "Amended", "Cancelled", "Ordered" ?

Answer

Aliostad picture Aliostad · Feb 8, 2011

You can try

        XmlDocument xml = new XmlDocument();
        xml.LoadXml("<Message><Event1 Operation=\"Amended\" Id=\"88888\"> Other XML Text</Event1></Message>");
        Debug.WriteLine(xml.DocumentElement.ChildNodes[0].Name);
        Debug.WriteLine(xml.DocumentElement.ChildNodes[0].Attributes["Operation"].Value);