我正在使用Automapper和带有Xml序列化的类来生成XML文件。
在此方法中,它将IEnumerable返回给Automapper,后者随后写出一系列<GenerationMethod>...</GenerationMethod>
XML元素。
但是它确实工作,如果它返回一个空的IEnumerable,因为没有找到结果,我得到的是这样的空XML标记:
<GenerationMethod />
是否有一种返回NULL的方式,以便不生成空的XML标记?
这里是方法。谢谢!
public static IEnumerable<GenerationMethod> GetGenerationMethod(this DungeonGrid monster)
{
var customMonster = monster.Stats
.Where(e => e.Stat.Category.IsActive);
if (monster.MonsterType.DestructionMethod.StartsWith("TEST"))
{
yield return new GenerationMethod(monster.MonsterType.DestructionMethod);
}
foreach (Stat in customMonster)
{
if (DungeonLookupByStatId.ContainsKey(customMonster.MonsterType.Id))
yield return DungeonLookupByStatId[customMonster.MonsterType.Id];
}
}
您的最终目标是在IEnumerable为空的情况下不在XML中包含GenerationMethod
标记。