简体   繁体   English

LINQ读取复杂的XML

[英]LINQ to read complex XML

LINQ to read XML LINQ读取XML

Below is my *.xml file having two sections: 以下是我的* .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>

As the results I wish to select (guid, fd_name_size) from the above two sections, as shown below 作为结果,我希望从以上两个部分中选择(guid,fd_name_size),如下所示

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

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


Here is my C# code 这是我的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;
        }

I get only the 'guid' (as a1) but do not know how also to select 'fd_name_size'. 我只得到“ guid”(作为a1),但不知道如何选择“ fd_name_size”。 Please help :o) 请帮忙:o)


lv1.Attribute("fd_name_size").Value

这应该够了吧。

You can do similarly you did it for "guid". 您可以类似地对“ 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;
        }

Thank you very much :o) The next block of *.xml data “complex_section” include also “section 'guid'”, but not 'fd_name_size', therefore NullReferenceException occurred. 非常感谢您:o)* .xml数据“ complex_section”的下一个块还包括“ section'guid'”,但不包括“ fd_name_size”,因此发生了NullReferenceException。 The *.xml above is just a part of a long *.xml file. 上面的* .xml只是长* .xml文件的一部分。 The C #codes below give me the correct values. 下面的C#代码为我提供了正确的值。 I love LinQ to XML. 我喜欢LinQ to XML。 Thanks :o) 谢谢: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
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM