-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
  • 178,094
  • 41
  • 278
  • 290
BumbleBee
  • 9,561
  • 19
  • 73
  • 118

2 Answers2

2

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

Daniel Hilgarth
  • 159,901
  • 39
  • 297
  • 411
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
  • 149,257
  • 27
  • 271
  • 318