How to Select a XML Node by Attribute

The ASP language uses XML to store and retrieve data for your website. When you have XML set up with attributes tags, you must use the "SelectNodes" function to get the XML tag and retrieve its data. You use this feature when you want to display only attribute data, or you want to search for a particular tag in the XML file.

Instructions

    • 1

      Open the Visual Studio software and open the project you want to edit. Double-click the code file you want to use to grab the XML data from the attribute tag.

    • 2

      Open the XML document and create a variable for the opened file. The following code opens a file named "customers.xml:"

      XmlDocument file = new XmlDocument();
      file.LoadXml("c:\customers.xml");

    • 3

      Get a list of XML tags that contain the attribute you want to retrieve. If more than one record contains the attribute, you retrieve more than one record. Add the following code to get the attribute named "female:"

      XmlNodeList nodes = file.SelectNodes("/Names/Name[@gender='female']");

    • 4

      Display the XML nodes' data. The following code displays each customer with the "female" attribute:

      foreach (XmlNode node in nodes)
      {
      Console.WriteLine(node.InnerText);
      }

Related Searches:

References

Comments

Related Ads

Featured