通过访问LINQ的到XML元素嵌套 [英] Accessing nested elements via Linq to Xml

查看:233
本文介绍了通过访问LINQ的到XML元素嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件,它看起来像这样

 <&MyObject的GT;
    < someinfo1 />
    < someinfo2>< / someinfo2>
    < someinfo3 />
    。
    。
    <项目>
        <&领域GT;
            < usefulinfo1>&东西LT; / usefulinfo1>
            < usefulinfo2>&东西LT; / usefulinfo2>
            < usefulinfo3>
                &所述;组>
                    < code>有的code< / code>
                    <描述>有的描述< /描述>
                < /组>
            < / usefulinfo3>
        < /田>
    < /项目>
    <项目>
        <&领域GT;
            < usefulinfo1>&东西LT; / usefulinfo1>
            < usefulinfo2>&东西LT; / usefulinfo2>
            < usefulinfo3>
                <名someattribute =>
                    <&初始化GT; someinit< / INIT>
                    <&姓GT;有的姓< /姓>
                < /名称>
                <名someattribute =>
                    <&初始化GT; someinit< / INIT>
                    <&姓GT;有的姓< /姓>
                < /名称>
                <公司someattribute =>
                    < coname> coname< / coname>
                    <国家>公司与国内LT; /国家>
                < /公司>
                <公司someattribute =>
                    < coname> coname< / coname>
                    <国家>公司与国内LT; /国家>
                < /公司>            < / usefulinfo3>
        < /田>
    < /项目>
    。
    。
    (更多标签命名项)< /为MyObject>

我曾尝试通过LINQ的XML来访问这些数据。
这就是我的实验是如此远...

 的XmlReader读卡器= XmlReader.Create(新StringReader(XMLTEXT));
        reader.Read();
        XDOC的XDocument = XDocument.Load(读卡器);
        reader.Close();
       IEnumerable的<&MyObject的GT;在xdoc.Root.Elements从我的数据=(项目)。后代(田)
        选择新为MyObject
        {
            usefulinfo1 =(字符串)i.Element(usefulinfo1),
            usefulinfo2 =(字符串)i.Element(usefulinfo2),
            usefulinfo3 =新的List< Usefulinfo3>(
                                                从i.Elements(usefulinfo3)usefulinfo3。后代()
                                                选择新Usefulinfo3
                                                    {
                                                        NAME =新的List< Usefulinfo3Name>(
                                                            从usefulinfo3.Elements名称(名称)。后代()
                                                            选择新Usefulinfo3Name
                                                            {
                                                                的init =(字符串)name.Element(INIT),
                                                                姓=(字符串)name.Element(姓)
                                                            }
                                                        )
                                                        公司= //使用公司类似的事情
                                                    }
                                                )
        };

那么,问题是,我还没有得到看正在加载的结果,即使我只是尝试搜索usefulinfo1和
    usefulinfo2而已,所以帮助下,我想就从这里开始,第二,因为我不知道如果我的方法将工作,我想
    知道是否有调试它,因为当我踏进数据调试时,它只是通过直线距离的一种方法,不显示
    我什么。
    如果你可以给我联系,我可以搜索这一点,那将是非常有用的,因为网站我发现至今只有秀
    我怎么去,直到item元素(我知道这部分应该是简单,但我只是还没有得到它的权利)。
    提高我的code方法的任何建议也欢迎。
    非常感谢你。


解决方案

这个例子对我的作品

  XDOC的XDocument = XDocument.Parse(XML);
VAR的结果= xDoc.Descendants(田)
    。选择(F =>新建
    {
        usefulinfo1 = f.Element(usefulinfo1)。价值,
        usefulinfo2 = f.Element(usefulinfo2)。价值,
        usefulinfo3 = f.Element(usefulinfo3)。元素(名称)
                        。选择(N =>
                            新{
                                姓= n.Element(姓)。价值,
                            })
                        .ToArray()    })
    .ToArray();


  

我要生成姓氏列表和次大写金额出现


  VAR名= xDoc.Descendants(姓)
    .GroupBy(X => x.Value)
    。选择(X =>新建{名称= x.Key,计数= x.Count()})
    .ToList();

I have a xml file that looks like this

<MyObject>
    <someinfo1/>
    <someinfo2></someinfo2>
    <someinfo3/>
    .
    .
    <Item>
        <fields>
            <usefulinfo1>something</usefulinfo1>
            <usefulinfo2>something</usefulinfo2>
            <usefulinfo3>
                <group>
                    <code>somecode</code>
                    <description>some description</description>
                </group>
            </usefulinfo3>
        </fields>
    </Item>
    <Item>
        <fields>
            <usefulinfo1>something</usefulinfo1>
            <usefulinfo2>something</usefulinfo2>
            <usefulinfo3>
                <name someattribute="">
                    <init>someinit</init>
                    <surname>some surname</surname>
                </name>
                <name someattribute="">
                    <init>someinit</init>
                    <surname>some surname</surname>
                </name>
                <company someattribute="">
                    <coname>coname</coname>
                    <country>company country</country>
                </company>
                <company someattribute="">
                    <coname>coname</coname>
                    <country>company country</country>
                </company>

            </usefulinfo3>
        </fields>   
    </Item>
    .
    .
    (More tags named Item)      

</MyObject>

I have tried to access this data via Linq XML. This is how my 'experiment' is being so far...

XmlReader reader = XmlReader.Create(new StringReader(xmltext));
        reader.Read();
        XDocument xdoc = XDocument.Load(reader);
        reader.Close();
       IEnumerable<MyObject> data = from i in xdoc.Root.Elements("Item").Descendants("fields")
        select new MyObject
        {
            usefulinfo1 = (string)i.Element("usefulinfo1"),
            usefulinfo2 = (string)i.Element("usefulinfo2"),
            usefulinfo3 = new List<Usefulinfo3>(
                                                from usefulinfo3 in i.Elements("usefulinfo3").Descendants()
                                                select new Usefulinfo3
                                                    {
                                                        name = new List<Usefulinfo3Name>(
                                                            from name in usefulinfo3.Elements("name").Descendants()
                                                            select new Usefulinfo3Name
                                                            {
                                                                init = (string)name.Element("init"),
                                                                surname = (string)name.Element("surname")
                                                            }
                                                        ),
                                                        company = //Do similar thing with company
                                                    }
                                                )
        };

Well, the problem is that I haven't even got to see the results being loaded even when I just try to search usefulinfo1 and usefulinfo2 only, so the help I would like would start here, second, since I don't know if my approach will work I would like to know if there is a way to debug it since when I step into data when debugging, it just passes straight away and doesn't show me anything. If you could send me links where I could search this, that would be useful, since the websites I've found so far only show me how to go until "Item" element (I know that this part should be simple but I just haven't got it right). Any suggestions of improving my code approach are also more than welcome. Thank you very much.

解决方案

This example works for me

XDocument xDoc = XDocument.Parse(xml);
var result = xDoc.Descendants("fields")
    .Select(f => new
    {
        usefulinfo1 = f.Element("usefulinfo1").Value,
        usefulinfo2 = f.Element("usefulinfo2").Value,
        usefulinfo3 = f.Element("usefulinfo3").Elements("name")
                        .Select(n=>
                            new {
                                surname = n.Element("surname").Value,
                            })
                        .ToArray()

    })
    .ToArray();

I want to generate a list with surnames and the amout of times they appear

var names = xDoc.Descendants("surname")
    .GroupBy(x => x.Value)
    .Select(x => new { Name = x.Key, Count = x.Count() })
    .ToList();

这篇关于通过访问LINQ的到XML元素嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆