how to display parent node attribute name in xmlDocument

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tookerello
    New Member
    • May 2010
    • 5

    how to display parent node attribute name in xmlDocument

    hi, im trying to display the attribute name of the parent node once the input matches a town name:

    my xml is:

    Code:
    <phonebook>
      <area code="022">
        <town>mallow</town>
      </area>
      <area code="023">
        <town>bandon</town>
        <town>bray</town>     
      </area>
      <area code="024">
        <town>youghal</town>    
      </area>
      <area code="025">
        <town>fermoy</town>    
      </area>
    </phonebook>
    and the method is:
    Code:
     private void btnGetNum_Click(object sender, EventArgs e)
    {
     XmlNodeList towns = doc.GetElementsByTagName("town");
       foreach (XmlNode town in towns)
       {
          if (txtInput.Text.Equals(town.ChildNodes[0].Value))
          {
           lstResult.Items.Add("area code is" +  ??????????);
       }
    }
    </phonebook>
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    An XmlNode object has a parent node. You should be able to get the parent node of the town, then parse the attributes present in the XmlAttributes property.

    Give that a try and let me know how it works out :)

    Comment

    • tookerello
      New Member
      • May 2010
      • 5

      #3
      the solution is to use town.ParentNode .Attributes["code"].Value

      Comment

      Working...