Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:06:02+00:00 2026-06-05T10:06:02+00:00

XDocument xDocument = XDocument.Load(…); IEnumerable<XElement> elements = xDocument .Element(lfm) .Element(events) .Elements(event); try { foreach

  • 0
XDocument xDocument = XDocument.Load("...");
IEnumerable<XElement> elements = xDocument
    .Element("lfm")
    .Element("events")
    .Elements("event");

try
{            
    foreach (XElement elm in elements)
    {
        comm.Parameters.AddWithValue("extID", elm.Element("id").Value  ?? "");
        comm.Parameters.AddWithValue("Title", elm.Element("title").Value ?? "");
        comm.Parameters.AddWithValue("HeadlineArtist", 
        elm.Element("artists").Element("headliner").Value ?? "");

but I want the value of the element “image” with the attribute “size=large”, I have been looking all night, and this is the closest I have come:

comm.Parameters.AddWithValue("LargeImage",
    elm.Descendants("image")
       .FirstOrDefault(i => (string)i.Attribute("size") == "large").Value);

Sample of the part of XML response:

<lfm status="ok">
    <events xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
            location="Chicago, United States" page="1" perPage="1"
            totalPages="341" total="341" festivalsonly="0" tag="">
        <event xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
            <id>3264699</id>
            <title>Iron And Wine</title>
            <artists>
                <artist>Iron And Wine</artist>
                <artist>Dr. John</artist>
                <headliner>Iron And Wine</headliner>
            </artists>
            <venue>
                <id>8915382</id>
                <name>Ravinia Festival</name>
                <location>
                    <city>Highland Park</city>
                    <country>United States</country>
                    <street>200 Ravinia Park Rd</street>
                    <postalcode>60035</postalcode>
                    <geo:point>
                        <geo:lat>42.15831</geo:lat>
                        <geo:long>-87.778409</geo:long>
                    </geo:point>
                </location>
                <url>http://www.last.fm/venue/8915382+Ravinia+Festival</url>
                <website>http://www.ravinia.org/</website>
                <phonenumber>847.266.5100</phonenumber>
                <image size="small">http://userserve-ak.last.fm/serve/34/63026487.jpg</image>
                <image size="medium">http://userserve-ak.last.fm/serve/64/63026487.jpg</image>
                <image size="large">http://userserve-ak.last.fm/serve/126/63026487.jpg</image>
                <image size="extralarge">http://userserve-ak.last.fm/serve/252/63026487.jpg</image>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:06:04+00:00Added an answer on June 5, 2026 at 10:06 am

    Try

    XElement result = elm.Descendants("image")
       .FirstOrDefault(el => el.Attribute("size") != null &&
                             el.Attribute("size").Value == "large");
    if (result != null) {
        process result.Value ...
    }
    

    Starting with C#6.0 (VS 2015), you can write:

    XElement result = elm.Descendants("image")
       .FirstOrDefault(el => el.Attribute("size")?.Value == "large");
    if (result != null) {
        process result.Value ...
    }
    

    A non-obvious alternative (as @RandRandom pointed out) is to cast the Attribute to string:

    XElement result = elm.Descendants("image")
       .FirstOrDefault(el => (string)el.Attribute("size") == "large");
    if (result != null) {
        process result.Value ...
    }
    

    This works, because because of XAttribute Explicit Conversion (XAttribute to String).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

C# var xArr = XDocument.Load(FileName) .Element(dataWorkers) .Elements(worker); int i = 0; foreach (XElement item
XDocument xdoc = XDocument.Load(file); IEnumerable<XElement> categories = xdoc.Descendants(Category); foreach (XElement category in categories) {
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
So i have this code: foreach (string xmlfilelisted in xmlFileList) { resultXml.Root.Add(XDocument.Load(xmlfilelisted).Root.Elements()); resultXml.Save(filepath); }
XDocument coordinates = XDocument.Load(http://feeds.feedburner.com/TechCrunch); System.IO.StreamWriter StreamWriter1 = new System.IO.StreamWriter(DestFilePath); foreach (var coordinate in coordinates.Descendants(guid))
I have the following: XDocument xdoc = XDocument.Load(C:\\myfile.xml); List<Tag> list = new List<Tag>(); foreach
i'm trying to load some elements from a xml file. but it XDocument.Load seems
As stated above, what's the difference between XElement.Load and XDocument.Load ? They both seemingly
I have the following XML which I load via XDocument.Load(uri) or XElement.Load(uri) . I
i am using linq-to-xml to search elements. var doc = XDocument.Load(reader); var ns =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.