0

I need to create a list of objects parsing an XML document. Issue appears when projecting to AdvertisementFullScreen

I receive this error:

{"Value cannot be null.\r\nParameter name: element"}

My code:

var advFs = loadedData.Element("xxx")
                           .Elements("advertisementsFullScreen")
                           .Elements("advertisement") // RESULT BELOW
                           .Select(x=>new AdvertisementFullScreen()
                           {
                               Id = (int)x.Element("id"),
                               ImageUrl = (string)x.Element("imageUrl"),
                               DisplayTime = new TimeSpan(0, 0, (int)x.Element("displayTime"))
                           }).ToList();

Result before projecting is:

[0] = <advertisement id="7001" imageUrl="C:/xxx/Test-data/data-offline/Assets/advertisementsFullScreen/1.png" displayTime="5" isSelfPromo="1"></advertisement>
1
  • Are you sure that casting something to int will convert it? You should use int.Parse or Convert.ToInt32 instead. Commented Jun 13, 2014 at 7:50

1 Answer 1

2

You're selecting Element(), but id, imageUrl, displayTime are not elements - they're attributes.

Modified query (just the important parts):

Id = (int)x.Attribute("id"),
ImageUrl = (string)x.Attribute("imageUrl"),
DisplayTime = new TimeSpan(0, 0, (int)x.Attribute("displayTime"))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.