我有以下xml相同的子元素(Filed),我想从每个Child元素获取值。从Xml查找具有相同元素名称的值
Sameple XML
<root xmlns="">
<books cat="F1" ISBN="01F187597" genre="Programming">
<Field name="Title" val="XML" />
<Field name="Publish Date" val="20010424" />
<Field name="Price" val="43.00" />
</books>
</root>
代码
XDocument xdoc = XDocument.Load("c:\\test6.xml");
var booksData = from book in xdoc.Descendants("root")
//I guess create this and do something with it
// let fieldElements = book.Descendants("Field")
select new Book
{
cat = book.Element("books").Attribute("cat").Value
,ISBN = book.Element("books").Attribute("ISBN").Value
,genre = book.Element("books").Attribute("genre").Value
,Price = "?"
,PublishDate="?"
,Title="?"
};
图书类
public class Book
{
public string cat {get;set;}
public string ISBN {get;set;}
public string genre {get;set;}
public string Title {get;set;}
public string PublishDate {get;set;}
public string Price { get; set; }
}
任何人有更好的办法了相同的代码,那么请张贴 – NETQuestion 2009-12-23 00:15:18