Based on your reply to my comment and your certainty in the question that you want to select nodes by index, here is a simple way to acheive it. Assuming your XML
have multiple DR
(with no names or other means to identify them). I use XPATH
here because in your case it might be easier than LINQ.
Assuming the XML is:
<Hospital>
<DR>
<Salary>1000</Salary>
<bonus> 3 </bonus>
</DR>
<DR>
<Salary>2000</Salary>
<bonus> 7 </bonus>
</DR>
<Nurse>
<Shift> </Shift>
</Nurse>
</Hospital>
You can select the second node using an XPATH instead as follows:
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlString);
XPathNavigator navigator = xmlDocument.CreateNavigator();
XPathNavigator node = navigator.SelectSingleNode("/Hospital/DR[2]/Salary");
if (node != null)
node.SetValue("new salary");
Please note the Xpath index [] is 1 based not 0 based. So DR[2] selects the second node.
EDIT:
And in answer to your second question, if you have a name
attribute in the DR
element. You can get and update that value much like the preceding example, all you need to change is the Xpath to point to that attribute, as such:
/Hospital/DR[2]/@name