以下のlinq to xmlクエリでは、list<string>
、DefaultValuesおよびValuesの2つのプロパティがあります。linq to xml空リスト<string>要素?
これらの要素のいずれかが空の場合、私は新しい、空のリストにLiteValueParameter
オブジェクトのプロパティを設定したいと思います:
Values = new List<string>();
代わりに、LINQクエリは、このLKE私に何かを与えている。
Values = new List<string>();
Values.Add("");
XMLに空の要素がある場合、空のアイテムがリストに追加されないようにする方法はありますか?
LINQのコード:
//linq query
List<LiteValueParameter> valParams = new List<LiteValueParameter>();
valParams = (from c in doc.Descendants("Parameters").Descendants("Parameter")
where (LiteParameterType)Enum.Parse(typeof(LiteParameterType), c.Element("ParameterType").Value, true) == LiteParameterType.Value
select new LiteValueParameter()
{
Id = c.Attribute("Id").Value,
DataType = Type.GetType(c.Element("DataType").Value, true),
DefaultValues = c.Elements("DefaultValues").Select(element => element.Value).ToList(),
DisplayText = c.Element("DisplayText").Value,
IsRequired = Convert.ToBoolean(c.Element("IsRequired").Value),
MinCount = Convert.ToInt32(c.Element("MinCount").Value),
MaxCount = Convert.ToInt32(c.Element("MaxCount").Value),
MinValue = c.Element("MinValue").Value,
MaxValue = c.Element("MaxValue").Value,
ParameterName = c.Element("ParameterName").Value,
Values = c.Elements("Values").Select(element => element.Value).ToList(),
ParameterType = (LiteParameterType)Enum.Parse(typeof(LiteParameterType), c.Element("ParameterType").Value, true),
DisplayType = c.Element("DisplayType").Value
}).ToList();
XMLコード:
<Parameters>
<Parameter Id="PermissionList">
<ParameterType>Value</ParameterType>
<ParameterName>Permissions</ParameterName>
<DisplayType>ListBox</DisplayType>
<DisplayText>Permissions</DisplayText>
<IsRequired>true</IsRequired>
<MinValue />
<MaxValue />
<DefaultValues />
<Values />
<DataType>System.String</DataType>
<MinCount>1</MinCount>
<MaxCount>1</MaxCount>
</Parameter>
</Parameters>
WHERE句の条件を変更しようとしましたか? –