pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

    2. <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 class="gsgoqi2" 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 以獲取子元素

        Parse XML with LINQ to get child elements(使用 LINQ 解析 XML 以獲取子元素)

        <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 class="eec8ocy" id='H7wLS'><tfoot id='H7wLS'></tfoot><dl id='H7wLS'><fieldset id='H7wLS'></fieldset></dl></div>

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

                    <tbody id='H7wLS'></tbody>
                  本文介紹了使用 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模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)
                    <tbody id='GUUdV'></tbody>
                  <legend id='GUUdV'><style id='GUUdV'><dir id='GUUdV'><q id='GUUdV'></q></dir></style></legend>
                    • <bdo id='GUUdV'></bdo><ul id='GUUdV'></ul>
                    • <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 class="ukigkya" 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>

                            主站蜘蛛池模板: 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 中山市派格家具有限公司【官网】 | 微波消解仪器_智能微波消解仪报价_高压微波消解仪厂家_那艾 | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 广东泵阀展|阀门展-广东国际泵管阀展览会 | 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 | 智慧农业|农业物联网|现代农业物联网-托普云农物联网官方网站 | 复合土工膜厂家|hdpe防渗土工膜|复合防渗土工布|玻璃纤维|双向塑料土工格栅-安徽路建新材料有限公司 | 杜康白酒加盟_杜康酒代理_杜康酒招商加盟官网_杜康酒厂加盟总代理—杜康酒神全国运营中心 | 东莞螺杆空压机_永磁变频空压机_节能空压机_空压机工厂批发_深圳螺杆空压机_广州螺杆空压机_东莞空压机_空压机批发_东莞空压机工厂批发_东莞市文颖设备科技有限公司 | 东莞压铸厂_精密压铸_锌合金压铸_铝合金压铸_压铸件加工_东莞祥宇金属制品 | 汝成内控-行政事业单位内部控制管理服务商 | 临海涌泉蜜桔官网|涌泉蜜桔微商批发代理|涌泉蜜桔供应链|涌泉蜜桔一件代发 | 亳州网络公司 - 亳州网站制作 - 亳州网站建设 - 亳州易天科技 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 垃圾压缩设备_垃圾处理设备_智能移动式垃圾压缩设备--山东明莱环保设备有限公司 | 天命文免费算命堂_自助算命_自由算命系统_长文周易 | 金属管浮子流量计_金属转子流量计厂家-淮安润中仪表科技有限公司 | 废水处理-废气处理-工业废水处理-工业废气处理工程-深圳丰绿环保废气处理公司 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 郑州宣传片拍摄-TVC广告片拍摄-微电影短视频制作-河南优柿文化传媒有限公司 | 泵阀展|阀门展|水泵展|流体机械展 -2025上海国际泵管阀展览会flowtech china | 周易算网-八字测算网 - 周易算网-宝宝起名取名测名字周易八字测算网 | 高低温老化试验机-步入式/低温恒温恒湿试验机-百科 | 换网器_自动换网器_液压换网器--郑州海科熔体泵有限公司 | 合肥仿石砖_合肥pc砖厂家_合肥PC仿石砖_安徽旭坤建材有限公司 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 | 九爱图纸|机械CAD图纸下载交流中心| 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 跨境物流_美国卡派_中大件运输_尾程派送_海外仓一件代发 - 广州环至美供应链平台 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 开平机_纵剪机厂家_开平机生产厂家|诚信互赢-泰安瑞烨精工机械制造有限公司 | 西门子气候补偿器,锅炉气候补偿器-陕西沃信机电工程有限公司 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 台式恒温摇床价格_大容量恒温摇床厂家-上海量壹科学仪器有限公司 | 搬运设备、起重设备、吊装设备—『龙海起重成套设备』 | 芜湖厨房设备_芜湖商用厨具_芜湖厨具设备-芜湖鑫环厨具有限公司 控显科技 - 工控一体机、工业显示器、工业平板电脑源头厂家 | 多功能真空滤油机_润滑油全自动滤油机_高效真空滤油机价格-重庆润华通驰 | 带压开孔_带压堵漏_带压封堵-菏泽金升管道工程有限公司 |