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

  • SEARCH
  • Home
  • 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 92255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:09:30+00:00 2026-05-10T23:09:30+00:00

I have written a LINQ to XML query that does what I want, but

  • 0

I have written a LINQ to XML query that does what I want, but it looks pretty ugly. I am wondering, how would you guys format the following query in a way that doesn’t look quite so garish?

Apologies if my example is a bit verbose.

The XML documents I am querying have the following structure:

<?xml version='1.0' encoding='iso-8859-1'?> <newsitem itemid='1' id='root' date='1996-08-20' xml:lang='en'>     <title>A title</title>     <headline>A headline</headline>     <dateline>A dateline</dateline>     <text>         Some text     </text>     <metadata>         <codes class=''>             <code code=''>                 <editdetail attribution=''/>             </code>         </codes>         <dc element='dc.date.created' value=''/>         <dc element='dc.publisher' value=''/>         <dc element='dc.date.published' value=''/>         <dc element='dc.source' value=''/>         <dc element='dc.creator.location' value=''/>         <dc element='dc.creator.location.country.name' value=''/>         <dc element='dc.source' value=''/>     </metadata> </newsitem> 

And corresponding LINQ query:

XElement dummy = new XElement('dummy'); var query = from article in newsdoc.Elements('newsitem').DefaultIfEmpty(dummy)             select new             {                 NewsItemID = (int)article.Attribute('itemid'),                 Date = (DateTime)article.Attribute('date'),                 Title = (string)article.Element('title'),                 Headline = (string)article.Element('headline'),                 ByLine = (string)article.Element('byline'),                 DateLine = (string)article.Element('dateline'),                 NewsText = (string)article.Element('text'),                 Publisher = (string)article.Elements('metadata').Elements('dc').Where(x => (string)x.Attribute('element') == 'dc.publisher').Attributes('value').DefaultIfEmpty().ElementAt(0),                 DatePublished = (DateTime)article.Elements('metadata').Elements('dc').Where(x => (string)x.Attribute('element') == 'dc.date.published').Attributes('value').DefaultIfEmpty().ElementAt(0),                 Source = (string)article.Elements('metadata').Elements('dc').Where(x => (string)x.Attribute('element') == 'dc.source').Attributes('value').DefaultIfEmpty().ElementAt(0),                 CreatorLocation = (string)article.Elements('metadata').Elements('dc').Where(x => (string)x.Attribute('element') == 'dc.creator.location').Attributes('value').DefaultIfEmpty().ElementAt(0),                 CreatorLocationCountryName = (string)article.Elements('metadata').Elements('dc').Where(x => (string)x.Attribute('element') == 'dc.creator.location.country.name').Attributes('value').DefaultIfEmpty().ElementAt(0),                 Codes = article.Elements('metadata').Elements('codes').Elements('code').Attributes('code').DefaultIfEmpty()             };  

Thanks!

  • 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. 2026-05-10T23:09:30+00:00Added an answer on May 10, 2026 at 11:09 pm

    The main ‘ugly’ there is the stuff at the bottom. I would probably add an extension method (or just a utility method) – something like:

        public static XAttribute GetMetadata(this XElement parent, string key)     {         return parent.Elements('metadata').Elements('dc')                  .FirstOrDefault(x => x.Attribute('element').Value == key)                  .Attribute('value');     } 

    then you should be able to use something like:

    Publisher = (string)article.GetMetadata('dc.publisher'); 

    (unchecked)

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

Sidebar

Related Questions

I have this linq query that works well (although it may be written better,
I have written a LINQ query with or condition in it, but its not
I have written what I thought to be a pretty solid Linq statement but
I have written this LINQ to xml query: Dim xd1 As XDocument = XDocument.Load(C:\doc1.xml)
I have written a class in python that implements __str__(self) but when I use
I have written code in C# that should transform an XML with the help
I have a string in linq which is loaded from xml, that attribute or
I have recently written a LINQ query to get a Dictionary containing the last
I have written a Linq to CRM query using CRM 2011 RC (v5) LINQ-to-CRM
I have written a WPF application that I want to port to Silverlight 2.

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.