C# Linq 中缺少后代

  • 本文关键字:后代 Linq c#
  • 更新时间 :
  • 英文 :


我是C#的新手。我有以下代码来读取/解析 xml。有时 xml 不会有"COMPU-INTERNAL-TO-PHYS"标签。那时,我得到了一些与对象相关的异常。阅读后代前如何检查?

 var query1 = xmlFile.Descendants("DATA-OBJECT-PROP").Select(level3 => new
            {
                DopID = level3.Attribute("ID").Value,
                DOPShortName = level3.Element("SHORT-NAME") == null ? "" : level3.Element("SHORT-NAME").Value,
                DOPLongName = level3.Element("LONG-NAME") == null ? "" : level3.Element("LONG-NAME").Value,
                //Category = level3.Element("COMPU-METHOD").Element("CATEGORY") == null ? "" : level3.Element("COMPU-METHOD").Element("CATEGORY").Value,
                CompuMethod =level3.Descendants("COMPU-METHOD").Select(x=> new {
             Category=x.Element("CATEGORY").Value,                        
CompuIntToPhys = level3.Descendants("COMPU-INTERNAL-TO-PHYS").Select(y=> new{
                            CompuScales=y.Descendants("COMPU-SCALES").Select(a=> new{
                                CompuRationalCoef=a.Descendants("COMPU-RATIONAL-COEFFS").Select(b=> new{
                                    CompuNumerator=b.Descendants("COMPU-NUMERATOR").Select(c=> new Item{
                                        v1 = c.Elements("V").Select(d=> d.Value).ToList()
                                    }),
                                    CompuDenominator = a.Descendants("COMPU-DENOMINATOR").Select(e => new
                                    {
                                        demoni = e.Element("V")
                                    }),
                                })
                            })
                        })
                }),
                phyType=level3.Descendants("PHYSICAL-TYPE").Select(pt=>new {
                    baseDataType=pt.Attribute("BASE-DATA-TYPE").Value,
                    precision=pt.Element("PRECISION").Value
                }),
                interConst = level3.Descendants("INTERNAL-CONSTR").Select(ic => new
                {
                    lowerLimit=ic.Element("LOWER-LIMIT").Value,
                    upperLimit=ic.Element("UPPER-LIMIT").Value
                }),
                unitRef = level3.Element("UNIT-REF").Attribute("ID-REF").Value
            });

.XML:

<DATA-OBJECT-PROP ID="DOP_UI">
                            <SHORT-NAME>DOP_UINT/SHORT-NAME>
                            <LONG-NAME>DEC</LONG-NAME>
                            <COMPU-METHOD>
                                <CATEGORY>IDEN</CATEGORY>
                            </COMPU-METHOD>
                            <DIAG-CODED-TYPE  BASE-DATA-TYPE="A_UI">
                                <BIT-LENGTH>7</BIT-LENGTH>
                                <BIT-MASK>7FA</BIT-MASK>
                            </DIAG-CODED-TYPE>
                            <PHYSICAL-TYPE BASE-DATA-TYPE="A_U2" />
                            <INTERNAL-CONSTR>
                                <LOWER-LIMIT>0</LOWER-LIMIT>
                                <UPPER-LIMIT>127</UPPER-LIMIT>
                            </INTERNAL-CONSTR>
                        </DATA-OBJECT-PROP>
您可以使用

DefaultIfEmpty

CompuIntToPhys = level3.Descendants("COMPU-INTERNAL-TO-PHYS")
                       .DefaultIfEmpty(defaultValue)
                       .Select(y => new { // ...

此方法返回

一个包含默认值的 IEnumerable(如果源为空); 否则,来源

如果 xml 文件包含此标记,则查询保持不变,如果没有,DefaultIfEmpty(defaultValue)将返回具有您提供的一些特殊值的单一实例IEnumerable<T>

最新更新