代码之家  ›  专栏  ›  技术社区  ›  Edward Tanguay

如何在具有linq to xml的元素之后获取第一个元素?

  •  7
  • Edward Tanguay  · 技术社区  · 16 年前

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

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

    但我该如何使它更精确,例如“获取 smartform元素之后的第一个元素 例如:

    //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>
    
    3 回复  |  直到 9 年前
        1
  •  3
  •   Andrew Hare    16 年前

    你想用 Descendants 轴方法,然后调用 FirstOrDefault 获取第一个元素的扩展方法。

    下面是一个简单的例子:

    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();
        }
    }
    
        2
  •  5
  •   ShuggyCoUk    16 年前

    如果你不知道 smartForm 是根元素,但仍要使用第一个这样的条目的标题文本:

    xml.DescendantsAndSelf("smartForm").Descendants("title").First().Value;
    

    这个 要求 其中有一个带有标题元素的smartform元素。

    如果要确保标题元素是 直系子代 在里面 智能表格 你可以使用:

    xml.DescendantsAndSelf("smartForm").Elements("title").First().Value;
    

    如果你不在乎的话 title 是并且只是想要第一个子元素,然后您将使用:

    xml.DescendantsAndSelf("smartForm").Elements().First().Value;
    
        3
  •  0
  •   Alexey Obukhov    9 年前

    我的任务是找到指定名字的第一个孩子。 如果XML使用命名空间,而不是

    e.Elements(name).FirstOrDefault()
    

    e.Elements().FirstOrDefault(i => i.Name.LocalName == name)