Skip to content

How to display parent node attribute name in xmlDocument

davy smith

Asked Feb 17, 2011, 5:01 AM EST1 answer
Share
LinkedInX
hi, im trying to display the attribute name of the parent node once the input matches a town name:

my xml is:

  1. <phonebook>
  2.   <area code="022">
  3.     <town>mallow</town>
  4.   </area>
  5.   <area code="023">
  6.     <town>bandon</town>
  7.     <town>bray</town>     
  8.   </area>
  9.   <area code="024">
  10.     <town>youghal</town>    
  11.   </area>
  12.   <area code="025">
  13.     <town>fermoy</town>    
  14.   </area>
  15. </phonebook>

and method is:

 

  1. private void btnGetNum_Click(object sender, EventArgs e)
  2. {
  3.  XmlNodeList towns = doc.GetElementsByTagName("town");
  4.    foreach (XmlNode town in towns)
  5.    {
  6.       if (txtInput.Text.Equals(town.ChildNodes[0].Value))
  7.       {
  8.        lstResult.Items.Add("area code is" +  ??????????);
  9.    }
  10. }

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.