How to cast XPathEvalute when it can be XElement or XAttribute?

前端 未结 4 1833
自闭症患者
自闭症患者 2021-02-20 06:29

So I have this code:

List prices =
                (from item in xmlDoc.Descendants(shop.DescendantXName)
                 select new PriceDet         


        
4条回答
  •  北海茫月
    2021-02-20 06:43

    XElement and XAttribute are both forms of XObject, so if a generic instance of type XObject will suffice for your needs, change your Cast to Cast.

    If that won't work for your specific situation, you make use of OfType or OfType to filter for one or the other, but that would require two passes over the input, one to filter for XElement and a second pass to filter for XAttribute.

提交回复
热议问题