XML文件是这样的:解析XML文件,并抢值
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
</catalog>
我需要的所有数据读取我的课:
public class Book
{
public string Id { get; set; }
public string Author { get; set; }
public string Title { get; set; }
public Genre Genre { get; set; }
public decimal Price { get; set; }
public DateTime PublishDate { get; set; }
public string Description { get; set; }
}
下面的代码不工作,我可以从节点抓取bookId,但不能标题,作者.. 如何实现该结果?这是我到目前为止有:
const string filePath = @"C:\Users\Michał\Desktop\books.xml";
XDocument xmlDoc = XDocument.Load(filePath);
var dupa = xmlDoc
.Descendants("book")
.Select(x => new Book()
{
Id = (string) x.Attribute("bookid"),
Title = (string) x.Attribute("title") // Title is empty after that code runs
}).ToList();
作者,标题等是Book的后代节点,而不是属性。 – localghost