Visualizzazione dei risultati da 1 a 3 su 3
  1. #1

    Linq to Xml - come fare se un elemento non esiste?

    Sto realizzando un'applicazione che legge feed rss, questa è la query linq:

    Codice PHP:
    var posts from item in XFeed.Descendants("item")
                
    select new
                {
                    
    Title item.Element("title").Value,
                    
    Description item.Element("description").Value,
                    
    Published item.Element("pubDate").Value,
                    
    Url item.Element("link").Value,
                    
    Enclosure item.Element("enclosure").Attribute("url").Value
                
    }; 
    Il problema è che non tutti i feed hanno l'elemento <enclosure> e per quelli che non lo supportano la query genera un'eccezione perché non trova quell'elemento.

    Come posso fare?
    C'è un modo per capire se un elemento esiste?

  2. #2

    Re: Linq to Xml - come fare se un elemento non esiste?

    ... scusa, mi sa che ho scritto una boiata ..
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

  3. #3

    Re: Linq to Xml - come fare se un elemento non esiste?

    ci riprovo ...
    Codice PHP:
    var posts from item in XFeed.Descendants("item")
                
    select new
                {
                    
    Title item.Element("title").Value,
                    
    Description item.Element("description").Value,
                    
    Published item.Element("pubDate").Value,
                    
    Url item.Element("link").Value,
                    
    Enclosure = (item.Element("enclosure") ?? new XElement("enclosure", new XAttribute("url"null))).Attribute("url").Value 
                
    }; 
    oppure:
    Codice PHP:
    Enclosure item.Element("enclosure") ? item.Element("enclosure").Attribute("url").Value null 
    :master:
    Formaldehyde a new Ajax PHP Zero Config Error Debugger

    WebReflection @WebReflection

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.