【问题标题】:LINQ to read complex XMLLINQ 读取复杂的 XML
【发布时间】:2017-09-19 13:48:15
【问题描述】:

LINQ 读取 XML

下面是我的 *.xml 文件,它有两个部分:


<sections>
    <section guid="112ff6b8-2609-4d19-b774-33ab951ee66a" last_change="1970-01-01T00:00:00.000" action="added"
             name="Concrete sections, Rectangle, 150x300" type="custom" fd-mat="3" fd_name_code="Concrete sections"
             fd_name_type="Rectangle" fd_name_size="150x300">
        <region_group>
            <region>
                <contour>
                    <edge type="line">
                        <point x="-0.075" y="-0.15" z="0"></point>
                        <point x="0.075" y="-0.15" z="0"></point>
                        <normal x="0" y="1" z="0"></normal>
                    </edge>
                    <edge type="line">
                        <point x="0.075" y="-0.15" z="0"></point>
                        <point x="0.075" y="0.15" z="0"></point>
                        <normal x="-1" y="0" z="0"></normal>
                    </edge>
                    <edge type="line">
                        <point x="0.075" y="0.15" z="0"></point>
                        <point x="-0.075" y="0.15" z="0"></point>
                        <normal x="0" y="-1" z="0"></normal>
                    </edge>
                    <edge type="line">
                        <point x="-0.075" y="0.15" z="0"></point>
                        <point x="-0.075" y="-0.15" z="0"></point>
                        <normal x="1" y="0" z="0"></normal>
                    </edge>
                </contour>
            </region>
        </region_group>
        <end></end>
    </section>
    <section guid="98948ace-afb2-400d-9d96-752f5fce40c4" last_change="1970-01-01T00:00:00.000" action="added"
             name="Concrete sections, Rectangle, 300x600" type="custom" fd-mat="3" fd_name_code="Concrete sections"
             fd_name_type="Rectangle" fd_name_size="300x600">
        <region_group>
            <region>
                <contour>
                    <edge type="line">
                        <point x="-0.15" y="-0.3" z="0"></point>
                        <point x="0.15" y="-0.3" z="0"></point>
                        <normal x="0" y="1" z="0"></normal>
                    </edge>
                    <edge type="line">
                        <point x="0.15" y="-0.3" z="0"></point>
                        <point x="0.15" y="0.3" z="0"></point>
                        <normal x="-1" y="0" z="0"></normal>
                    </edge>
                    <edge type="line">
                        <point x="0.15" y="0.3" z="0"></point>
                        <point x="-0.15" y="0.3" z="0"></point>
                        <normal x="0" y="-1" z="0"></normal>
                    </edge>
                    <edge type="line">
                        <point x="-0.15" y="0.3" z="0"></point>
                        <point x="-0.15" y="-0.3" z="0"></point>
                        <normal x="1" y="0" z="0"></normal>
                    </edge>
                </contour>
            </region>
        </region_group>
        <end></end>
    </section>

作为结果我希望从上面两个部分中选择(guid, fd_name_size),如下图

112ff6b8-2609-4d19-b774-33ab951ee66a,150x300

98948ace-afb2-400d-9d96-752f5fce40c4, 300x600


这是我的 C# 代码

        string a1 = "will be guid"; string a2 = "will be fd_name_size"; // to test

        var secBC = from lv1 in xDoc.Descendants("section")
                    select new
                    {
                        Header = lv1.Attribute("guid").Value
                       // How to select "fd_name_size" ????
                    };

        foreach (var lv1 in secBC)
        {
            a1 = lv1.Header;
        }

我只得到'guid'(作为a1)但不知道如何选择'fd_name_size'。请帮忙:o)


【问题讨论】:

    标签: c# linq-to-xml


    【解决方案1】:
    lv1.Attribute("fd_name_size").Value
    

    这应该可以解决问题。

    【讨论】:

      【解决方案2】:

      你可以做与“guid”类似的事情。

      var secBC = from lv1 in xdoc.Descendants("section")
                          select new
                          {
                              Header = lv1.Attribute("guid").Value,
                              NameSize= lv1.Attribute("fd_name_size").Value                            
                          };
      
              foreach (var lv1 in secBC)
              {
                   a1 = lv1.Header;
                   a2 = lv1.NameSize;
              }
      

      【讨论】:

      • 亲爱的 Nazmul Haque,感谢您的帮助 :o) 但我得到“NullReferenceException was unhandled” for *select new {...; Namesize=lv1.Attribute("fd_name_size").Value }
      • 那么你应该检查你的xml文件。可能有某种错误。检查所有部分中的“fd_name_size”名称。你也可以用 lv1.Attribute("fd_name_size")?.Value 代码代替 lv1.Attribute("fd_name_size").Value
      • 亲爱的 Nazmul Haque,非常感谢 :o) *.xml 数据“complex_section”的下一个块还包括“部分‘guid’”,但不包括“fd_name_size”,因此发生 NullReferenceException。上面的 *.xml 只是长 *.xml 文件的一部分。下面的 C #codes 给了我正确的值。我喜欢 LinQ to XML。谢谢 :o)
      【解决方案3】:

      非常感谢 :o) 下一个 *.xml 数据块“complex_section”还包括“section ‘guid’”,但不包括 ‘fd_name_size’,因此发生 NullReferenceException。上面的 *.xml 只是长 *.xml 文件的一部分。下面的 C #codes 给了我正确的值。我喜欢 LinQ to XML。谢谢 :o)

              List<string> guID = new List<string>();
              List<string> name = new List<string>();
              var secBC = from lv1 in xDoc.Descendants("section")
                          .Where(lv1 => (string)lv1.Attribute("fd_name_size") != null)
                          select new
                          {
                              Header = lv1.Attribute("guid").Value,
                              NameSize = lv1.Attribute("fd_name_size").Value  
                          };
      
              foreach (var lv1 in secBC)
              {
                  guID.Add(lv1.Header);   // [0] 112ff6b8-2609-4d19-b774-33ab951ee66a, OK 
                                          // [1] 98948ace-afb2-400d-9d96-752f5fce40c4, OK
      
                  name.Add(lv1.NameSize); // [0] 150x300, OK 
                                          // [1] 300x600, OK
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多