XML ファイルから匿名オブジェクトを作成しています。今まで、
commentary.Elements("Commentator")
常に値を持っていたので、null をチェックする必要はありませんでした。ただし、それを削除する必要がありましたが、その行を読み取ろうとすると失敗します。
私はコードを見ていますが、匿名オブジェクトのプロパティに選択されているため、何を変更すればよいかわかりません。
var genericOfflineFactsheet = new
{
Commentary = (from commentary in doc.Elements("Commentary")
select new
{
CommentaryPage = (string)commentary.Attribute("page"),
BusinessName = (string)commentary.Attribute("businessName"),
Commentator = (from commentator in commentary.Elements("Commentator")
select new CommentatorPanel // ASP.NET UserControl
{
CommentatorName = (string)commentator.Attribute("name"),
CommentatorTitle = (string)commentator.Attribute("title"),
CommentatorCompany = (string)commentator.Attribute("company")
}).FirstOrDefault()
}).FirstOrDefault()
commentary.Elements("Commentator")
問題は、値がある場合があるため、行を完全に削除できないことです。この問題は以前に対処されていると確信していますが、どうすればよいかわかりません。何か案は?