0

xml構造の例:

<item>
   <title>Foo</title>
   <description>Boo</description>
   <image>Poo</image>
</item>

私のコードは次のとおりです。

var rssFeeds =
              from feed in rssXML.Descendants("item")             
              select new
              {                  
                  Title = feed.Element("title").Value,                  
                  Description = feed.Element("description").Value,                                                                                                 
                  Image= feed.Element("image").Value, 
              };        

問題は、上記のコードに「title」要素が存在するかどうかを確認する方法です...存在しない場合は、他の要素を取得します

4

1 に答える 1

1

明示的XElementから文字列への変換(nullXElement参照をnull文字列参照に変換する)をnull合体演算子と組み合わせて使用​​できます。

Title = (string) feed.Element("title") ?? (string) feed.Element("otherElement")
于 2012-08-12T07:31:24.390 に答える