2009-08-03 8 views
7

Z tym kodem można uzyskać tytuł Spośród następującym pliku XML:Jak mogę uzyskać pierwszy element po elemencie z LINQ-do-XML?

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

Ale jak mogę uczynić go bardziej dokładny, na przykład „Dostać pierwszy element po elemencie SmartForm, np coś takiego:

//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> 

Odpowiedz

3

chcesz użyć metody Descendants osi, a następnie wywołać metodę FirstOrDefault rozszerzenie do dostać pierwszy element

Oto prosty przykład:.

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(); 
    } 
} 
5

Aby dodać lekko odpowiedź Andrzeja, jeśli nie wiem, czy smartForm jest elementem głównym, ale nadal chcą tekst tytuł pierwszego takiego wpisu użyłbyś:

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

Ten wymaga że tam być element smartForm z elementem tytułu gdzieś w nim.

Jeśli chcesz, aby zapewnić, że element tytuł był natychmiastowe dziecko w smartForm można użyć:

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

Jeśli nie obchodzi, co nazwa title było i po prostu chcieliśmy pierwszy sub element następnie użyjesz:

xml.DescendantsAndSelf("smartForm").Elements().First().Value; 
+0

dobre wytłumaczenie. e.Elementy (nazwa) .First() jest tym, czego potrzebuję. –

0

Moje zadanie polegało na znalezieniu pierwszego dziecka o określonej nazwie. Jeśli xml używa nazw, niż zamiast

e.Elements(name).FirstOrDefault() 

Write

e.Elements().FirstOrDefault(i => i.Name.LocalName == name) 
Powiązane problemy