5

LINQ を使用して、ATOM フィードの作成者ノードから「名前」フィールドを選択しようとしています。次のように、必要なすべてのフィールドを取得できます。

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

このシナリオで著者 - >名前フィールドを選択するにはどうすればよいですか?

4

2 に答える 2

5

あなたは基本的に欲しい:

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

ただし、作成者要素または名前要素が欠落している場合に適切なアクションを簡単に実行できるように、それを追加のメソッドでラップすることをお勧めします。複数の著者がいる場合、どうしたいかについても考えたいと思うかもしれません。

フィードには author 要素も含まれる場合があります。もう 1 つ注意が必要です。

于 2008-11-18T22:42:36.373 に答える
3

それは次のようなものかもしれません:

        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
                         )
                    };
于 2008-11-18T23:09:24.917 に答える