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

前端 未结 4 1899
自闭症患者
自闭症患者 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

    Before you make the cast you can check for the type using a code like this:

    XElement e = item as XElement;
    XAttribute a = item as XAttribute;
    
    if(e != null)
       //item is of type XElement
    else
      //item is of type XAttribute
    

提交回复
热议问题