Domanda

Sto provando a selezionare il " nome " campo dal nodo dell'autore in un feed ATOM utilizzando LINQ. Posso ottenere tutti i campi di cui ho bisogno in questo modo:

XDocument stories = XDocument.Parse(xmlContent);
XNamespace xmlns = "http://www.w3.org/2005/Atom";
var story = from entry in stories.Descendants(xmlns + "entry")
            select new Story
            {
                Title = entry.Element(xmlns + "title").Value,
                Content = entry.Element(xmlns + "content").Value
            };

Come farei per selezionare l'autore - > campo nome in questo scenario?

È stato utile?

Soluzione

In pratica vuoi:

entry.Element(xmlns + "author").Element(xmlns + "name").Value

Ma potresti volerlo racchiudere in un metodo aggiuntivo in modo da poter facilmente intraprendere le azioni appropriate se mancano gli elementi dell'autore o del nome. Potresti anche pensare a cosa vuoi che accada se c'è più di un autore.

Il feed potrebbe anche avere un elemento autore ... solo un'altra cosa da tenere a mente.

Altri suggerimenti

Potrebbe essere qualcosa del genere:

        var story = from entry in stories.Descendants(xmlns + "entry")
                    from a in entry.Descendants(xmlns + "author")
                    select new Story
                    {
                        Title = entry.Element(xmlns + "title").Value,
                        Content = entry.Element(xmlns + "subtitle").Value,
                        Author = new AuthorInfo(
                            a.Element(xmlns + "name").Value,
                            a.Element(xmlns + "email").Value,
                            a.Element(xmlns + "uri").Value
                         )
                    };
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top