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> </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
任何帮助深表感谢