コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】XmlAttribute が null ではない場合にのみ Value を返す拡張メソッド

ソースコード

using System.Xml;

public static class XmlExt
{
    public static string GetValueOrDefault
    ( 
        this XmlAttribute self, 
        string            defaultValue = "" 
    )
    {
        return self != null ? self.Value : defaultValue;
    }
}

使い方

var attr = node.Attributes[ "name" ];
var value = attr.GetValueOrDefault();