C#解析带属性的XML

以下面的xml文件为例:
<?xml version="1.0"?>
<info>
	<book id="b1" lang="en">
		<name>c++</name>
		<price>570</price>
	</book>
	<book id="b2" lang="en">
		<name>c#</name>
		<price>250</price>
	</book>
</info>

使用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#解析带属性的XML)