简体   繁体   English

Linq-XML总是那么混乱吗?

[英]Is Linq-XML always so messy?

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)
             };

The structure of the XML is as follows: XML的结构如下:

<Items>
    <Item>
        <Id></Id>
        <Name></Name>
        <Description></Description>
        <Price></Price>
    </Item>
</Items>

Id, and price are both integer values. Id和价格都是整数值。 Name and description are strings. 名称和描述是字符串。

I've found Linq to XML great for what I've used it for, this is just a snippet. 我发现Linq to XML非常适合我用它,这只是一个片段。 But, on the other hand I get the feeling it should or could be cleaner. 但是,另一方面,我感觉它应该或可能更清洁。 The casting seems the most obvious issue in this snippet. 在这个片段中,转换似乎是最明显的问题。

Any advice? 有什么建议?

Actually it would be better IMO to cast than to call int.Parse . 实际上,施放比调用int.Parse更好的IMO。 Here's how I would write your query: 这是我写你的查询的方式:

string id = itemId.ToString(); // We don't need to convert it each time!

var subset = from item in document.Descendants("Id")
             where item.Value == id
             let parent = item.Parent
             select new PurchaseItem
             {
                 Id = (int) parent.Element("Id"),
                 Name = (string) parent.Element("Name"),
                 Description = (string) parent.Element("Description"),
                 Price = (int) parent.Element("Price")
             };

I assume that you have an "Items" node as well? 我假设你有一个“Items”节点?

You could do something like this, assuming that you are loading the document using XElement.Load() 您可以执行类似这样的操作,假设您使用XElement.Load()加载文档

var subset = from item in document.Elements("Item")
             where item.Element("Id").Value == itemId.ToString()
             select new PurchaseItem() {
                 Id = int.Parse(item.Element("Id").Value),
                 Name = item.Element("Name").Value,
                 Description = item.Element("Description").Value,
                 Price = int.Parse(item.Element("Price").Value)
             };

Not a lot better, but much easier to read! 不是更好,但更容易阅读!

In your example you can tidy up a little bit by finding the <Item/> element rather than the <Id/> to avoid getting the Parent each time: 在您的示例中,您可以通过找到<Item/>元素而不是<Id/>来整理一点,以避免每次都获取Parent

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

考虑为PurchaseItem编写一个带有XML元素的新构造函数,这样你就可以编写:

select new PurchaseItem(item.Parent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM