【发布时间】:2010-12-02 15:47:59
【问题描述】:
<books>
<book name="Christmas Cheer" price="10" />
<book name="Holiday Season" price="12" />
<book name="Eggnog Fun" price="5" special="Half Off" />
</books>
我想用 linq 解析这个,我很好奇其他人用什么方法来处理特殊的。我目前的处理方式是:
var books = from book in booksXml.Descendants("book")
let Name = book.Attribute("name") ?? new XAttribute("name", string.Empty)
let Price = book.Attribute("price") ?? new XAttribute("price", 0)
let Special = book.Attribute("special") ?? new XAttribute("special", string.Empty)
select new
{
Name = Name.Value,
Price = Convert.ToInt32(Price.Value),
Special = Special.Value
};
我想知道是否有更好的方法来解决这个问题。
谢谢,
- 贾里德
【问题讨论】:
标签: c# linq linq-to-xml