XElement Path Validation

Posted by cw on Stack Overflow See other posts from Stack Overflow or by cw
Published on 2010-03-24T03:35:34Z Indexed on 2010/03/24 3:43 UTC
Read the original article Hit count: 550

Filed under:
|

Hello,

I am trying to validate Elements and Attributes exist in an XElement. Basically, I was wondering if anyone had a generic way to check if a give path is null. I don't have access to System.Xml.XPath (doing this for compact framework).

Basically what I have is:

<root value"1000">
  <element1>test<element1>
  <element2>1<element2>
  ....
  <element30>
    <subElement1>stuff</subElement1>
  </element30>
</root>

Now I know you can "if this is null do this and that". But since there is upwards of 30 elements that can be under root, which are optional elements, I need a way to grab the value if it exists and convert it to the correct type (which I know) in a nice compact way.

Any suggestions?

© Stack Overflow or respective owner

Related posts about xelement

Related posts about LINQ