• <bdo id='rx2mR'></bdo><ul id='rx2mR'></ul>

      <i id='rx2mR'><tr id='rx2mR'><dt id='rx2mR'><q id='rx2mR'><span id='rx2mR'><b id='rx2mR'><form id='rx2mR'><ins id='rx2mR'></ins><ul id='rx2mR'></ul><sub id='rx2mR'></sub></form><legend id='rx2mR'></legend><bdo id='rx2mR'><pre id='rx2mR'><center id='rx2mR'></center></pre></bdo></b><th id='rx2mR'></th></span></q></dt></tr></i><div id='rx2mR'><tfoot id='rx2mR'></tfoot><dl id='rx2mR'><fieldset id='rx2mR'></fieldset></dl></div>
    1. <legend id='rx2mR'><style id='rx2mR'><dir id='rx2mR'><q id='rx2mR'></q></dir></style></legend>

      <small id='rx2mR'></small><noframes id='rx2mR'>

    2. <tfoot id='rx2mR'></tfoot>

    3. 带有检查空元素的 XML 到 LINQ

      时间:2023-08-28
      1. <small id='G32di'></small><noframes id='G32di'>

        • <tfoot id='G32di'></tfoot>

          <i id='G32di'><tr id='G32di'><dt id='G32di'><q id='G32di'><span id='G32di'><b id='G32di'><form id='G32di'><ins id='G32di'></ins><ul id='G32di'></ul><sub id='G32di'></sub></form><legend id='G32di'></legend><bdo id='G32di'><pre id='G32di'><center id='G32di'></center></pre></bdo></b><th id='G32di'></th></span></q></dt></tr></i><div id='G32di'><tfoot id='G32di'></tfoot><dl id='G32di'><fieldset id='G32di'></fieldset></dl></div>
            • <bdo id='G32di'></bdo><ul id='G32di'></ul>
              <legend id='G32di'><style id='G32di'><dir id='G32di'><q id='G32di'></q></dir></style></legend>
                <tbody id='G32di'></tbody>

              1. 本文介绍了带有检查空元素的 XML 到 LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我面临的情况是使用 Linq 将 XML 文档解析为对象.在解析过程中,我正在检查以确保元素不为空,然后再继续解析它们的值.有没有办法简化这句话?

                The situation I am faced with is parsing an XML document into an object using Linq. During the parse I am checking to make sure Elements are not null before proceeding to parse out their values. Is there anyway to simplify this statement?

                  var variable = (from x in xdoc.Descendants("Root")
                                 select new AccountingResponse
                                 {      
                                 NetCharge = x.Element("Charges") != null && x.Element("Charges").Element("NetCharge") != null ? x.Element("Charges").Element("NetCharge").Value : "0",
                                 TotalCharge = x.Element("Charges") != null && x.Element("Charges").Element("TotalCharge") != null ? x.Element("Charges").Element("TotalCharge").Value : "0"
                                 }).SingleOrDefault();
                

                总而言之,我不想继续检查每行是否存在节点.我知道我可以在解析之前测试该节点是否存在,但可能还有其他数据需要解析以创建 AccountingResponse,我想避免一次只解析一部分 XML 的 if 语句.

                To summarize, I do not want to continue to check if the nodes exist on each line. I know I can test to see if the node exists prior to the parsing, but there may be other data that needs parsed to create the AccountingResponse and I want to avoid if statements that only parse a portion of the XML out at a time.

                或者也许我做错了,有更好的方法!

                Or perhaps I'm doing this completely wrong and there's a better way!

                推荐答案

                一个简单的选择是使用 Elements 而不是 Element - 这将返回一个零长度的序列如果元素不存在.所以你可以使用:

                One simple option is to use Elements rather than Element - that will return a zero-length sequence if the element isn't present. So you can use:

                from x in xdoc.Descendants("Root")
                select new AccountingResponse
                {      
                    NetCharge = x.Elements("Charges")
                                 .Elements("NetCharge")
                                 .Select(y => (int) y)
                                 .FirstOrDefault(),
                    TotalCharge = x.Elements("Charges")
                                   .Elements("TotalCharge")
                                   .Select(y => (int) y)
                                   .FirstOrDefault(),
                }).SingleOrDefault();
                

                (请注意,您的原始代码不会编译,因为 Value 是一个字符串,而 0 是一个 int...)

                (Note that your original code wouldn't compile, as Value is a string whereas 0 is an int...)

                这篇关于带有检查空元素的 XML 到 LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:在 C# 中读取带有未闭合标签的 XML 下一篇:读取 XML 时忽略空格

                相关文章

                最新文章

                <legend id='7Tipo'><style id='7Tipo'><dir id='7Tipo'><q id='7Tipo'></q></dir></style></legend>

              2. <i id='7Tipo'><tr id='7Tipo'><dt id='7Tipo'><q id='7Tipo'><span id='7Tipo'><b id='7Tipo'><form id='7Tipo'><ins id='7Tipo'></ins><ul id='7Tipo'></ul><sub id='7Tipo'></sub></form><legend id='7Tipo'></legend><bdo id='7Tipo'><pre id='7Tipo'><center id='7Tipo'></center></pre></bdo></b><th id='7Tipo'></th></span></q></dt></tr></i><div id='7Tipo'><tfoot id='7Tipo'></tfoot><dl id='7Tipo'><fieldset id='7Tipo'></fieldset></dl></div>
                <tfoot id='7Tipo'></tfoot>

                  • <bdo id='7Tipo'></bdo><ul id='7Tipo'></ul>
                1. <small id='7Tipo'></small><noframes id='7Tipo'>