XML to LINQ with Checking Null Elements

后端 未结 3 1234
不知归路
不知归路 2021-01-13 16:21

The situation I am faced with is parsing an XML document into an object using Linq. During the parse I am checking to make sure Elements are not null before proceeding to p

3条回答
  •  不知归路
    2021-01-13 16:43

    You can replace

    x.Element("nodeName") != null : int.Parse(x.Element("nodeName").Value) : 0
    

    with

    (int)x.Element("nodeName")
    

    It works for string, int, double, decimal, bool, uint, DateTime and Nullable of those as well.

提交回复
热议问题