XmlTextReader reader = new XmlTextReader("example.xml"); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.HasAttributes) { while (reader.MoveToNextAttribute()) { if (reader.Name == "id") { Console.WriteLine("id: " + reader.ReadAttributeValue()); } } } } }
XmlTextReader reader = new XmlTextReader("example.xml"); DictionaryThis example reads an XML file named "example.xml" and loops through each element in the file. When it encounters an element with attributes, it loops through each attribute and adds it to a Dictionary object. The keys of the dictionary are the attribute names and the values are the attribute values. Package library: System.Xmlattributes = new Dictionary (); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.HasAttributes) { while (reader.MoveToNextAttribute()) { attributes.Add(reader.Name, reader.ReadAttributeValue()); } } } }