如何使用LINQ-to-XML获得元素之后的第一个元素? [英] How can I get the first element after an element with LINQ-to-XML?

查看:56
本文介绍了如何使用LINQ-to-XML获得元素之后的第一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码,我可以从以下XML文件中获取标题:

With this code I can get the title out of the following XML file:

var xml = XElement.Load (@"C:\\test\\smartForm-customersMain.xml");
string title = xml.Element("title").Value;

但是我如何使其更精确,例如在smartForm元素之后获取第一个元素,例如:

But how do I make it more exact, e.g. "get the first element after the smartForm element, e.g. something like this:

//PSEUDO-CODE:
string title = xml.Element("smartForm").FirstChild("title");

XML:

<?xml version="1.0" encoding="utf-8" ?>
<smartForm idCode="customersMain">
    <title>Customers Main222</title>
    <description>Generic customer form.</description>
    <area idCode="generalData" title="General Data">
        <column>
            <group>
                <field idCode="anrede">
                    <label>Anrede</label>
                </field>
                <field idCode="firstName">
                    <label>First Name</label>
                </field>
                <field idCode="lastName">
                    <label>Last Name</label>
                </field>
            </group>
        </column>
    </area>
    <area idCode="address" title="Address">
        <column>
            <group>
                <field idCode="street">
                    <label>Street</label>
                </field>
                <field idCode="location">
                    <label>Location</label>
                </field>
                <field idCode="zipCode">
                    <label>Zip Code</label>
                </field>
            </group>
        </column>
    </area>
</smartForm>

推荐答案

您要使用Descendants轴方法,然后调用FirstOrDefault扩展方法以获取第一个元素.

You want to use the Descendants axis method and then call the FirstOrDefault extension method to get the first element.

这是一个简单的例子:

using System;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        String xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
            <smartForm idCode=""customersMain"">
                <title>Customers Main222</title>
                <description>Generic customer form.</description>
                <area idCode=""generalData"" title=""General Data"">
                <column>
                    <group>
                    <field idCode=""anrede"">
                        <label>Anrede</label>
                    </field>
                    <field idCode=""firstName"">
                        <label>First Name</label>
                    </field>
                    <field idCode=""lastName"">
                        <label>Last Name</label>
                    </field>
                    </group>
                </column>
                </area>
                <area idCode=""address"" title=""Address"">
                <column>
                    <group>
                    <field idCode=""street"">
                        <label>Street</label>
                    </field>
                    <field idCode=""location"">
                        <label>Location</label>
                    </field>
                    <field idCode=""zipCode"">
                        <label>Zip Code</label>
                    </field>
                    </group>
                </column>
                </area>
            </smartForm>";

        XElement element = XElement.Parse(xml)
            .Descendants()
            .FirstOrDefault();
    }
}

这篇关于如何使用LINQ-to-XML获得元素之后的第一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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