2

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") 問題は、値がある場合があるため、行を完全に削除できないことです。この問題は以前に対処されていると確信していますが、どうすればよいかわかりません。何か案は?

4

4 に答える 4

2

null チェックを追加するだけです。(以下のコードは、優れたテスターである必要があります。コメンテーターのあるものとないものがあります)

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf-8", "yes"),
    new XComment("Example"),
new XElement("Commentarys",
        new XElement("Commentary",
            new XAttribute("page", "2"),
            new XAttribute("businessName", "name"),
            new XElement("Commentator",
               new XAttribute("name", "name"),
                new XAttribute("title", "title")
            )
         )
        ,
        new XElement("Commentary",
            new XAttribute("page", "3"),
            new XAttribute("businessName", "name2")

            )
    )
    );
var genericOfflineFactsheet = new
{
    Commentary = (
           from commentary in doc.Elements()
                .First().Elements("Commentary")
           select new
          {
              CommentaryPage = (string)commentary.Attribute("page"),
              BusinessName = (string)commentary.Attribute("businessName"),
              Commentator = (from commentator in commentary.Elements("Commentator")
                             where commentator != null //<-----you need to add this line
                             select new // ASP.NET UserControl
                             {
                                 CommentatorName = (string)commentator.Attribute("name"),
                                 CommentatorTitle = (string)commentator.Attribute("title"),
                                 CommentatorCompany = (string)commentator.Attribute("company")
                             }

                             ).FirstOrDefault()
          }
 ).FirstOrDefault()
};
于 2010-10-12T21:06:27.410 に答える
1

これは、 ??を検討するケースです。(合体)演算子。

null ?? x --> x

この場合、空の Enumerable に結合することで回避できます。

于 2010-10-12T21:24:48.583 に答える
1

未テスト...

次のようなものはどうですか:

  XElement xe = doc.Elements("Commentary").FirstOrDefault();
  Commentary = xe == null ? null :
    select new { ....snip....
于 2010-10-12T21:15:35.937 に答える
0

私は?XMLElement が生成されないようにするには、次のように演算子を使用します。

  • 使用するオブジェクトが null でない場合、配列内の単一の新しい XElement を返します
  • null の場合は、Enumerable.Empty を返します

例:

var xml = new XElement("Root",
    myobject == null ? Enumerable.Empty<XElement>() : <= empty IEnumerable if it is null
                       new []                         <= a array with a XElement
                           { 
                               new XElement("myobject", 
                                   new XAttribute("Name", myobject.Name),
                                   new XAttribute("Type", myobject.Type)
                               ...)
                           },
    ...);

この場合、XElement の作成に関与するオブジェクトが null の場合、XElement は生成されません。

于 2013-04-19T05:45:35.773 に答える