LINQ读取复杂的XML

时间:2017-09-19 13:48:15

标签: c# linq-to-xml

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&#39; (作为a1)但不知道如何选择'fd_name_size'。请帮忙:o)

3 个答案:

答案 0 :(得分:0)

lv1.Attribute("fd_name_size").Value

这应该可以解决问题。

答案 1 :(得分:0)

你可以为#34; guid&#34;。

做同样的事情
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;
        }

答案 2 :(得分:0)

非常感谢:o)下一个* .xml数据块“complex_section”也包含“section'guid'”,但不包括'fd_name_size',因此发生了NullReferenceException。上面的* .xml只是long * .xml文件的一部分。下面的C#代码给出了正确的值。我喜欢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
        }