c# - Linq to Object/XML 其中元素不存在

标签 c# xml linq linq-to-xml

var doc3 = XDocument.Load(@"C:\Projects\ScanBandConfigTesting\ScanBandConfigTesting\ScanBandConfigSmall.xml");

var scanBand = new ScanBand()
{
    ListOfForms = (from form in doc3.Descendants("form")
                    select new ScanBandForm()
                    {
                        FormTypes = form.Attribute("types").Value,
                        ScanBandNumber = form.Attribute("number").Value,
                        ListOfRows = (from row in form.Descendants("row")
                                        select new ScanBandRow()
                                        {
                                            AllowSpaces = row.Element("allowSpaces").Value.ToLower() == "true",
                                            SplitCharacter = row.Element("splitCharacter").Value,
                                            ListOfColumns = (from column in row.Descendants("column")
                                                            select new ScanBandColumn()
                                                            {
                                                                AlwaysKey = column.Element("allwaysKey").IsEmpty ? false : column.Element("allwaysKey").Value.ToLower() == "true",
                                                                DataTypeString = column.Element("dataType").IsEmpty ? string.Empty : column.Element("dataType").Value,
                                                                MatchingFieldName = column.Element("matchingFieldName").IsEmpty ? string.Empty : column.Element("matchingFieldName").Value,
                                                                NonField = column.Element("nonField").IsEmpty ? false : column.Element("nonField").Value.ToLower() == "true",
                                                                RegularExpressionString = column.Element("regularExpression").IsEmpty ? string.Empty : column.Element("regularExpression").Value,
                                                            }).ToList()
                                        }).ToList()
                    }).ToList()
};

XML

<scanBand>
  <form types="FormName" number="1">
    <row>
      <allowSpaces>false</allowSpaces>
      <splitCharacter>&#32;</splitCharacter>
      <column>
        <matchingFieldName>FirstField</matchingFieldName>
        <dataType>CB</dataType>
        <regularExpression></regularExpression>
        <allwaysKey>false</allwaysKey>
        <nonField>false</nonField>
      </column>
      <column>
        <matchingFieldName>SecondField</matchingFieldName>
        <dataType>CB</dataType>
        <regularExpression></regularExpression>
        <allwaysKey>false</allwaysKey>
        <nonField>false</nonField>
      </column>
      <column>
        <matchingFieldName>ThirdField</matchingFieldName>
        <dataType>CB</dataType>
        <regularExpression></regularExpression>
        <!--<allwaysKey></allwaysKey>-->
        <nonField>true</nonField>
      </column>
    </row>
  </form>
</scanBand>

目标是当 .xml 文件中的某个元素不存在时,不会将其炸毁。我尝试使用 .Any() 但没有成功。

我宁愿不使用 foreach 进行迭代,而宁愿坚持使用 LINQ

非常感谢任何帮助

最佳答案

不要使用 Value property 获取属性或元素的值。如果缺少节点,您将获得异常。当您转换节点(例如转换为字符串)时,如果缺少节点,您将获得该类型的默认值。您也可以使用 ??运算符为缺少的字符串节点提供您自己的默认值(默认情况下您将获得 null)。

result = (string)column.Element("dataType") ?? String.Empty

对 bool 值使用相同的技巧 - 我得到 Nullable<bool>如果是 null (缺少节点)然后我分配 false如果不是 null ,然后节点的值成功分配给不可为空的属性:

 ListOfForms = 
     (from form in doc3.Descendants("form")
      select new ScanBandForm() {
          FormTypes = (string)form.Attribute("types"),
          ScanBandNumber = (string)form.Attribute("number"),
          ListOfRows = 
              (from row in form.Descendants("row")
               select new ScanBandRow() {
                   AllowSpaces = (bool?)row.Element("allowSpaces") ?? false,
                   SplitCharacter = (string)row.Element("splitCharacter"),
                   ListOfColumns = 
                      (from column in row.Descendants("column")  
                       select new ScanBandColumn() {
                            AlwaysKey = (bool?)column.Element("allwaysKey") ?? false,
                            DataTypeString = (string)column.Element("dataType") ?? String.Empty,
                            MatchingFieldName = (string)column.Element("matchingFieldName") ?? String.Empty,
                            NonField = (bool?)column.Element("nonField") ?? false,
                            RegularExpressionString = (string)column.Element("regularExpression") ?? String.Empty,
                       }).ToList()
                }).ToList()
      }).ToList();

关于c# - Linq to Object/XML 其中元素不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14860831/

相关文章:

c# - Unity3d - 如何获取 Animator 的图层 --> 子状态 --> 状态名称哈希?

c# - 将逗号分隔值转换为字典

python - 在Python中使用for循环时重置变量

c# - 关于 LINQ 查询中透明标识符的句法映射

c# - LINQ查询数据表,加入多个选择

c# - 使用对象初始化器 - Resharper 建议

C# 从sql server导出到excel

c# - 使用 csv 进行数据驱动测试,两次失败

c# - 使用 xdocument 创建 xml

xml - 阅读和理解 R 中的 XML