简体   繁体   中英

Accessing xml elements using LINQ to XML

I have a xml document like this and I need to access the "employees", "employee" elements so I am trying to use linq's XDocument class to get the employee elements but it always returns empty value.

Sample xml:

<organization>
   <metadata>
   </metadata>
   <main>
     <otherInfo>
     </otherInfo>
     <employeeInfo>
      <employees>
        <employee>
           <id>1</id>
           <name>ABC</name>
        </employee>
        <employee>
           <id>2</id>
           <name>ASE</name>
        </employee>
        <employee>
           <id>3</id>
           <name>XYZ</name>
        </employee>
      </employees>
     </employeeInfo>
    </main>
</organization>

C# code:

XDocument xDoc = XDocument.Parse(xmlString);
var allEmployees = from d in xDoc.Descendants("employeeInfo")
               from ms in d.Elements("employees")                                   
               from m in ms.Elements("employee")
               select m;

It kind of depends on what information you need. Your select returns an IEnumerable list. This code will print out each employee

string xmlString = @"<organization>
                           <metadata>
                           </metadata>
                           <main>
                             <otherInfo>
                             </otherInfo>
                             <employeeInfo>
                              <employees>
                                <employee>
                                   <id>1</id>
                                   <name>ABC</name>
                                </employee>
                                <employee>
                                   <id>2</id>
                                   <name>ASE</name>
                                </employee>
                                <employee>
                                   <id>3</id>
                                   <name>XYZ</name>
                                </employee>
                              </employees>
                             </employeeInfo>
                            </main>
                        </organization>";

        XDocument xDoc = XDocument.Parse(xmlString);
        var allEmployees = from d in xDoc.Descendants("employeeInfo")
           from ms in d.Elements("employees")                                   
           from m in ms.Elements("employee")
           select m;

        foreach (var emp in allEmployees) {
            Console.WriteLine(emp);
        }
        Console.Read();
XDocument xDoc = XDocument.Parse(xmlString);
var allEmployees = (from r in xDoc.Descendants("employee")
                   select new
                   {
                       Id = r.Element("id").Value,
                       Name = r.Element("name").Value
                   }).ToList();

foreach (var r in allEmployees)
{
    Console.WriteLine(r.Id + " " + r.Name);
}

Just use Descendants("Employee");

XDocument xDoc = XDocument.Parse(xmlString);
var allEmployees = xDoc.Descendants("employee").ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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