-2


Can somebody help me figure out what is wrong with the code below?

Messages = (
        from k in j.Descendants(xmlns + BLConst.MessageElement)
        select new KWI.Common.CLUE.BusinessEntities.Message()
        {
            type = (k.Attribute(BLConst.TypeElement) != null) ? (k.Attribute(BLConst.TypeElement).Value).ToString() : string.Empty,
            MessageText = (k.Element( xmlns + BLConst.MessageElement).Value).ToString()
        }
    ).ToList()

I get an error at select new kwi....Message(){ .. }

Thanks

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
BumbleBee
  • 10,429
  • 20
  • 78
  • 123

2 Answers2

2

Either k.Attribute(...).Value is null or k.Element(...) is null or k.Element(...).Value is null.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
2

Your MessageText selection is off - k is already a message element, yet you are trying to select a child message element from it which doesn't exist - just take the value:

MessageText = k.Value;
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335