<small id='H7wLS'></small><noframes id='H7wLS'>

      <tfoot id='H7wLS'></tfoot>

      <legend id='H7wLS'><style id='H7wLS'><dir id='H7wLS'><q id='H7wLS'></q></dir></style></legend>
      <i id='H7wLS'><tr id='H7wLS'><dt id='H7wLS'><q id='H7wLS'><span id='H7wLS'><b id='H7wLS'><form id='H7wLS'><ins id='H7wLS'></ins><ul id='H7wLS'></ul><sub id='H7wLS'></sub></form><legend id='H7wLS'></legend><bdo id='H7wLS'><pre id='H7wLS'><center id='H7wLS'></center></pre></bdo></b><th id='H7wLS'></th></span></q></dt></tr></i><div id='H7wLS'><tfoot id='H7wLS'></tfoot><dl id='H7wLS'><fieldset id='H7wLS'></fieldset></dl></div>

        • <bdo id='H7wLS'></bdo><ul id='H7wLS'></ul>

        使用 LINQ 解析 XML 以获取子元素

        时间:2023-08-28

          <small id='AJBuH'></small><noframes id='AJBuH'>

              <tbody id='AJBuH'></tbody>
          1. <tfoot id='AJBuH'></tfoot>

            • <i id='AJBuH'><tr id='AJBuH'><dt id='AJBuH'><q id='AJBuH'><span id='AJBuH'><b id='AJBuH'><form id='AJBuH'><ins id='AJBuH'></ins><ul id='AJBuH'></ul><sub id='AJBuH'></sub></form><legend id='AJBuH'></legend><bdo id='AJBuH'><pre id='AJBuH'><center id='AJBuH'></center></pre></bdo></b><th id='AJBuH'></th></span></q></dt></tr></i><div id='AJBuH'><tfoot id='AJBuH'></tfoot><dl id='AJBuH'><fieldset id='AJBuH'></fieldset></dl></div>
                <bdo id='AJBuH'></bdo><ul id='AJBuH'></ul>

                1. <legend id='AJBuH'><style id='AJBuH'><dir id='AJBuH'><q id='AJBuH'></q></dir></style></legend>
                  本文介绍了使用 LINQ 解析 XML 以获取子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  <?xml version="1.0" standalone="yes"?>
                  <CompanyInfo>
                       <Employee name="Jon" deptId="123">
                        <Region name="West">
                          <Area code="96" />
                        </Region>
                        <Region name="East">
                          <Area code="88" />
                        </Region>
                       </Employee>
                  </CompanyInfo>  
                  
                  public class Employee
                  {
                      public string EmployeeName { get; set; }
                      public string DeptId { get; set; }
                      public List<string> RegionList {get; set;}
                  }
                  
                  public class Region
                  {
                      public string RegionName { get; set; }
                      public string AreaCode { get; set; }
                  }
                  

                  我正在尝试读取这个 XML 数据,到目前为止我已经尝试过:

                  I am trying to read this XML data, so far I have tried this:

                  XDocument xml = XDocument.Load(@"C:data.xml");
                  var xElement = xml.Element("CompanyInfo");
                  if (xElement != null)
                      foreach (var child in xElement.Elements())
                      {
                          Console.WriteLine(child.Name);  
                          foreach (var item in child.Attributes())
                          {
                              Console.WriteLine(item.Name + ": " + item.Value);
                          }
                  
                          foreach (var childElement in child.Elements())
                          {
                              Console.WriteLine("--->" + childElement.Name);
                              foreach (var ds in childElement.Attributes())
                              {
                                  Console.WriteLine(ds.Name + ": " + ds.Value);
                              }
                              foreach (var element in childElement.Elements())
                              {
                                  Console.WriteLine("------->" + element.Name);
                                  foreach (var ds in element.Attributes())
                                  {
                                      Console.WriteLine(ds.Name + ": " + ds.Value);
                                  }
                              }
                          }                
                      }
                  

                  这使我能够获取每个节点,其属性名称和值,因此我可以将这些数据保存到数据库中的相关字段中,但这似乎是一个冗长的方式,并且不灵活,例如,如果 XML 结构发生变化,所有这些 foreach 语句都需要重新访问,那么这种方式也很难过滤数据,我需要编写某些 if 语句来过滤数据(例如,仅从 West 获取员工等...)

                  This enables me to get each node, its attribute name and value and so I can save these data into the relevant field in database, but this seems a long winded way and not flexible, for instance if the XML structure changes all those foreach statements needs revisiting, also it is difficult to filter the data this way, I need to write certain if statements to filter the data (e.g get employees from West only etc...)

                  我一直在寻找一种更灵活的方式,使用 linq,如下所示:

                  I was looking for a more flexible way, using linq, something like this:

                  List<Employees> employees =
                                (from employee in xml.Descendants("CompanyInfo")
                                 select new employee
                                 {
                                     EmployeeName = employee.Element("employee").Value,
                                     EmployeeDeptId = ?? get data,
                                     RegionName = ?? get data,
                                     AreaCode = ?? get data,,
                                 }).ToList<Employee>();
                  

                  但我不确定如何从子节点获取值并应用过滤(仅获取某些员工).这可能吗?任何帮助表示赞赏.

                  But I am not sure how I can get the values from the child nodes and apply the filtering (to get the certain employees only). Is this possible? Any help is appreciated.

                  谢谢

                  推荐答案

                  var employees = (from e in xml.Root.Elements("Employee")
                                   let r = e.Element("Region")
                                   where (string)r.Attribute("name") == "West"
                                   select new Employee
                                   {
                                       EmployeeName = (string)e.Attribute("employee"),
                                       EmployeeDeptId = (string)e.Attribute("deptId"),
                                       RegionName = (string)r.Attribute("name"),
                                       AreaCode = (string)r.Element("Area").Attribute("code"),
                                   }).ToList();
                  

                  但当 XML 文件结构发生变化时,仍需要修改查询.

                  But it will still require query revision when XML file structure changes.

                  编辑

                  查询每个员工的多个区域:

                  Query for multiple regions per employee:

                  var employees = (from e in xml.Root.Elements("Employee")
                                   select new Employee
                                   {
                                       EmployeeName = (string)e.Attribute("employee"),
                                       DeptId = (string)e.Attribute("deptId"),
                                       RegionList = e.Elements("Region")
                                                     .Select(r => new Region {
                                                         RegionName = (string)r.Attribute("name"),
                                                         AreaCode = (string)r.Element("Area").Attribute("code")
                                                     }).ToList()
                                   }).ToList();
                  

                  然后,您可以仅筛选来自给定区域的员工列表:

                  You can then filter the list for employees from given region only:

                  var westEmployees = employees.Where(x => x.RegionList.Any(r => r.RegionName == "West")).ToList();
                  

                  这篇关于使用 LINQ 解析 XML 以获取子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:删除没有子节点的父节点 下一篇:解析内部标签及其值

                  相关文章

                  最新文章

                2. <legend id='GUUdV'><style id='GUUdV'><dir id='GUUdV'><q id='GUUdV'></q></dir></style></legend>
                  • <bdo id='GUUdV'></bdo><ul id='GUUdV'></ul>
                3. <i id='GUUdV'><tr id='GUUdV'><dt id='GUUdV'><q id='GUUdV'><span id='GUUdV'><b id='GUUdV'><form id='GUUdV'><ins id='GUUdV'></ins><ul id='GUUdV'></ul><sub id='GUUdV'></sub></form><legend id='GUUdV'></legend><bdo id='GUUdV'><pre id='GUUdV'><center id='GUUdV'></center></pre></bdo></b><th id='GUUdV'></th></span></q></dt></tr></i><div id='GUUdV'><tfoot id='GUUdV'></tfoot><dl id='GUUdV'><fieldset id='GUUdV'></fieldset></dl></div>

                  <small id='GUUdV'></small><noframes id='GUUdV'>

                    1. <tfoot id='GUUdV'></tfoot>