相关疑难解决方法(0)

Linq-XML总是那么混乱吗?

var subset = from item in document.Descendants("Id")
             where item.Value == itemId.ToString()
             select new PurchaseItem() {
                 Id = int.Parse(item.Parent.Element("Id").Value),
                 Name = item.Parent.Element("Name").Value,
                 Description = item.Parent.Element("Description").Value,
                 Price = int.Parse(item.Parent.Element("Price").Value)
             };
Run Code Online (Sandbox Code Playgroud)

XML的结构如下:

<Items>
    <Item>
        <Id></Id>
        <Name></Name>
        <Description></Description>
        <Price></Price>
    </Item>
</Items>
Run Code Online (Sandbox Code Playgroud)

Id和价格都是整数值.名称和描述是字符串.

我发现我用过Linq to XML的东西很棒,这只是一个片段.但是,另一方面,我感觉它应该或可能更清洁.在这个片段中,转换似乎是最明显的问题.

有什么建议?

.net c# linq linq-to-xml

6
推荐指数
1
解决办法
1157
查看次数

标签 统计

.net ×1

c# ×1

linq ×1

linq-to-xml ×1