linq to xml 空列表;元素?
在下面的 linq to xml 查询中,我有 2 个属性,即 list
、DefaultValues 和 Values。
如果这些元素中的任何一个为空,我想将 LiteValueParameter
对象的该属性设置为一个新的空列表:
Values = new List<string>();
相反,linq 查询给我类似这样的东西:
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>
In the linq to xml query below, I have 2 properties that are a list<string>
, DefaultValues and Values.
If either of these elements are empty, I would like to set that property of the LiteValueParameter
object to a new, empty list:
Values = new List<string>();
Instead, the linq query is giving me something lke this:
Values = new List<string>();
Values.Add("");
Is there any way to prevent an empty item being added to the list if I have an empty element in my XML?
Linq code:
//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 Code:
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你可以像这样解决它:
不过这种方法感觉非常“hacky”。相反,我会将您的 XML 更改为具有您查询的
DefaultValue
元素:这更自然,现在您可以像这样编写查询,
如果您只是有,这将返回一个空集合
I suppose you could work around it like this:
This approach feels very "hacky" though. Instead I would change your XML to have a
DefaultValue
element that you query instead:This is much more natural and now you can just write your query like
This will return an empty collection if you just have