XmlDocument doc = new XmlDocument(); doc.Load("example.xml"); XmlNodeList nodes = doc.GetElementsByTagName("employee"); foreach(XmlNode node in nodes) { Console.WriteLine(node.Attributes["name"].Value); }
XmlDocument doc = new XmlDocument(); doc.LoadXml("In this example, we create an XML document programmatically and use GetElementsByTagName to find all elements with the tag name "book". We then use SelectSingleNode to find the "title" and "author" nodes within each "book" node, and print out the values of these nodes. Package library: System.Xml."); XmlNodeList nodes = doc.GetElementsByTagName("book"); foreach(XmlNode node in nodes) { XmlNode title = node.SelectSingleNode("title"); XmlNode author = node.SelectSingleNode("author"); Console.WriteLine(title.InnerText + " by " + author.InnerText); } The Great Gatsby F. Scott Fitzgerald To Kill a Mockingbird Harper Lee