开发者

How to know when to cast to an Xelement or XAttribute?

开发者 https://www.devze.com 2023-01-09 02:43 出处:网络
I have a situation where 开发者_如何学Pythonan end user can enter an XPath to access a value in some XML. I’m using a line of code similar to the one below:

I have a situation where 开发者_如何学Pythonan end user can enter an XPath to access a value in some XML. I’m using a line of code similar to the one below:

IEnumerable e = (IEnumerable)importDocument.XPathEvaluate(theXPath);

As the Xpath could return an Attribute or an Element, what I need to know is how can I interpret ‘e’ in the above example to decide to cast to an XElement or XAttribute ?


Something like?

XElement element = e.Current as XElement;
XAttribute attrib = e.Current as XAttribute;

if(element != null)
   //Is Element, use element
else
   //Is Attribute, use attrib

Or do you want to cast the Enumerator?

0

精彩评论

暂无评论...
验证码 换一张
取 消