2008-11-21 5 views
5

J'ai écrit une requête LINQ to XML qui fait ce que je veux, mais elle est plutôt moche. Je me demande, comment voulez-vous mettre en forme la requête suivante d'une manière qui n'a pas l'air si criarde?Des conseils sur la meilleure façon de structurer/formater une requête LINQ to XML?

Excuses si mon exemple est un peu verbeux.

Les documents XML J'Interrogation la structure suivante:

<?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> 

Et correspondant requête LINQ:

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() 
      }; 

Merci!

+0

Que dit le le .DefaultIfEmpty() accomplit? –

Répondre

4

Le principal "laid" il y a le truc en bas. Je probablement ajouter une méthode d'extension (ou juste une méthode utilitaire) - quelque chose comme:

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

alors vous devriez être en mesure d'utiliser quelque chose comme:

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

(non cochée)

Questions connexes