匿名オブジェクトに追加する前に、このXelementがnullでないかどうかを確認するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/3919010

質問

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") します 値を持っています。この問題は以前に処理されたと確信していますが、何をすべきかわかりません。何か案は?

役に立ちましたか?

解決

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

他のヒント

テストされていない...

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

  XElement xe = doc.Elements("Commentary").FirstOrDefault();
  Commentary = xe == null ? null :
    select new { ....snip....

これは私が考慮するかもしれない場合です ?? (合体) オペレーター。

null ?? x --> x

この場合、空の列挙可能に合体することで逃げることができます。

私はそれを使いますか?この方法でオペレーターXMLelementが生成されるのを防ぐ:

  • 使用するオブジェクトがnullでない場合、配列に1つの新しいXelementを返します
  • それがnullの場合、列挙性を返します

例:

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)
                               ...)
                           },
    ...);

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top