LinqToXml的优化

时间:2016-10-07 09:40:16

标签: c# xml linq

有人能告诉我这是否正确或是否有更快/更清洁的方法?

//I load p as a xDoc so I can use linq to xml
    XDocument myDoc = XDocument.Parse(p);
    // retrieve each "Item" node except if orderedQuantity is 0 or name is "nullorderitem"
    IEnumerable<XElement> orderedRes = from item in myDoc.Descendants("Item")
                                       where ((double)item.Element("orderedQuantity") > 0 &&     item.Element("ResourceId").Name != "NULLOrderItem")
                                       select item;

    foreach (XElement xelem in orderedRes)
    {
        if(xelem.Element("Name").Value.ToLower() == "oneofmyvalueIneed" 
            || xelem.Element("Name").Value.ToLower() == "anotherone"
            || xelem.Element("Name").Value.ToLower() == "yetanother")
        {
            FieldProperties elem = new FieldProperties();
            elem.Fieldname = xelem.Element("Name").Value;
            elem.Fieldvalue = xelem.Element("OrderedQuantity").Value;
            lElem.Add(elem);
        }
    }

知道lElem是FieldProperties的列表,而FieldProperties是一个类似这样的类:

FieldProperties
string fieldname
string fieldvalue 

和p是一个看起来像

的字符串
<items>
<item>
 <resourceid>blablabla</resourceid>
 <somerandomtag>whocares</somerandomtag>
 <orderedquantity>0.0</orderedquantity>
</item>
<item>
 <resourceid>oneofmyvalueIneed</resourceid>
 <somerandomtag>whocares</somerandomtag>
 <orderedquantity>1.0</orderedquantity>
</item>
<item>
 <resourceid>yetanother</resourceid>
 <somerandomtag>whocares</somerandomtag>
 <orderedquantity>0.0</orderedquantity>
</item>
</items>

1 个答案:

答案 0 :(得分:1)

您可以使用if方法改进.Contains语句。

var validNames = new List<string> {"oneofmyvalueIneed","anotherone","yetanother"}

XDocument myDoc = XDocument.Parse(p);
var result = from item in myDoc.Descendants("Item")
      where ((double)item.Element("orderedQuantity") > 0 &&                   
            item.Element("ResourceId").Name != "NULLOrderItem") && // See Charles's comment about this line
            validNames.Contains(iten.Element("Name").Value.ToLower())
      select item;

foreach (var item in orderedRes)
{
    FieldProperties elem = new FieldProperties();
    elem.Fieldname = xelem.Element("Name").Value;
    elem.Fieldvalue = xelem.Element("OrderedQuantity").Value;
    lElem.Add(elem);
}

然后您也可以用{/ 1>替换foreach

select new FieldProperties
{
    Fieldname = item.Element("Name").Value,
    Fieldvalue = xelem.Element("OrderedQuantity").Value
};

将所有内容添加到一起+在访问子元素时进行一些优化,如下所示:

var validNames = new List<string> {"oneofmyvalueIneed","anotherone","yetanother"}

XDocument myDoc = XDocument.Parse(p);
var result = from item in myDoc.Descendants("Item")
      let value = item.Element("orderedQuantity")
      let name = iten.Element("Name").Value.ToLower()
      where ((double)value > 0 &&                   
            item.Element("ResourceId").Name != "NULLOrderItem") && // See Charles's comment about this line
            validNames.Contains(name)
      select new FieldProperties
      {
          Fieldname = name
          Fieldvalue = value
      };