我有自己开发的下一个代码。它有效,但如果在XML中有3条记录,它将显示三条记录,但它们都将具有与最后一条记录相同的信息。
//Load file and puts it on a IEnumerable interface
IEnumerable<MObject> MObjects = XDocument.Load(v_urlDataConnection)
.Element("MTestData").Descendants("book")
.Select(x => new MObject
{
Title = x.Element("title").Value,
Artist = x.Element("artist").Value,
Company = x.Element("company").Value,
Country = x.Element("country").Value,
Year = x.Element("year").Value,
Price = float.Parse(x.Element("price").Value)
}).ToList();
xml是:
<MTestData>
<book>
<title>Scenes</title>
<artist>Dream Theater</artist>
<company>Vertigo</company>
<country>USA</country>
<year>1999</year>
<price>19.99</price>
</book>
<book>
<title>Falling</title>
<artist>Dream Theater</artist>
<company>3</company>
<country>3</country>
<year>3</year>
<price>3</price>
</book>
<book>
<title>Images and Words</title>
<artist>Dream Theater</artist>
<company>Elektra</company>
<country>USA</country>
<year>1991</year>
<price>15</price>
</book>
</MTestData>