C#解析带属性的XML

以下面的xml文件为例:


	
		c++
		570
	
	
		c#
		250
	

使用System.Xml.XmlDocument来解析,如下:以下面的xml文件为例:以下面的xml文件为例:
XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(@"file.xml");
            foreach (XmlElement book in xmlDocument.SelectNodes(@"info/book"))
            {
                // if you know attribute name simply use GetAttribute e.g.
                Console.WriteLine("id value: {0}.", book.GetAttribute("id"));
                // if you don't know attribute names you can loop e.g.
                foreach (XmlAttribute attribute in book.Attributes)
                {
                    Console.WriteLine("attribute with name {0} has value {1}.", attribute.Name, attribute.Value);
                }
            }

你可能感兴趣的:(C#)