Linq to Object/XML,其中元素没有';不存在

本文关键字:不存在 元素 Object to XML Linq | 更新日期: 2023-09-27 17:58:55

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

任何帮助都非常感谢

Linq to Object/XML,其中元素没有';不存在

不要使用Value属性来获取属性或元素的值。若缺少节点,则会出现异常。当您将节点强制转换(例如转换为字符串(时,如果缺少节点,您将获得该类型的默认值。此外,您还可以使用??运算符为丢失的字符串节点提供自己的默认值(默认情况下,您将获得null(。

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

与布尔值使用的技巧相同-我得到Nullable<bool>,如果它是null(节点丢失(,则我分配false,如果它不是null,则节点的值成功分配给不可为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();