当前位置: 首页 > 编程语言 > C#

使用LINQ解析XML获取子元素分享

时间:2023-04-10 18:05:01 C#

使用LINQ解析XML获取子元素publicclassEmployee{publicstringEmployeeName{get;放;}publicstringDeptId{get;放;}publicListRegionList{get;放;}}publicclassRegion{publicstringRegionName{get;放;}publicstringAreaCode{get;放;我正在尝试读取此XML数据,到目前为止我已经尝试过:XDocumentxml=XDocument.Load(@"C:data.xml");varxElement=xml.Element("公司信息");if(xElement!=null)foreach(varchildinxElement.Elements()){Console.WriteLine(child.Name);foreach(variteminchild.Attributes()){Console.WriteLine(item.Name+":"+item.Value);}foreach(varchildElementinchild.Elements()){Console.WriteLine("--->"+childElement.Name);foreach(vardsinchildElement.Attributes()){Console.WriteLine(ds.Name+":"+ds.Value);}foreach(varelementinchildElement.Elements()){Console.WriteLine("-------->"+element.Name);foreach(vardsinelement.Attributes()){Console.WriteLine(ds.Name+":"+ds.Value);这使我能够获取每个节点及其属性名称和值,因此我可以将这些数据保存到数据库中的相关字段中,但这似乎很长而且不灵活,例如,如果XML结构全部更改这些foreach语句需要重新访问,而且很难以这种方式过滤数据,我需要编写一些if语句来过滤数据(例如从西部获取员工等......)我正在寻找一种更灵活的方式,使用linq,如下所示:data,AreaCode=??获取数据,,}).ToList();但我不知道如何从子节点获取价值并应用过滤(仅适用于某些员工)是否可能?任何帮助表示赞赏。谢谢varemployees=(fromeinxml.Root.Elements("Employee")letr=e.Element("Region")where(string)r.Attribute("name")=="West"selectnewEmployee{EmployeeName=(string)e.Attribute("employee"),EmployeeDeptId=(string)e.Attribute("deptId"),RegionName=(string)r.Attribute("name"),AreaCode=(string)r.Element("面积").Attribute("代码"),}).ToList();但是当XML文件结构发生变化时,它仍然需要修改查询。编辑查询每个员工的多个字段:varemployees=(fromeinxml.Root.Elements("Employee")selectnewEmployee{EmployeeName=(string)e.Attribute("employee"),DeptId=(string)e.Attribute("deptId"),RegionList=e.Elements("Region").Select(r=>newRegion{RegionName=(string)r.Attribute("name"),AreaCode=(string)r.Element("Area").Attribute("code")}).ToList()}).ToList();然后,您可以仅针对给定区域的员工过滤列表:varwestEmployees=employees.Where(x=>x.RegionList.Any(r=>r.RegionName=="West")).ToList();您可以跟踪结构:fromemployeeinxml.Element("CompanyInfo")//mustberoot.Elements("Employee")//onlydirectlychildrenofCompanyInfoorlessstrictfromemployeeinxml.Descendants("Employee")//任何级别的所有员工并获取您想要的信息:selectnewEmployee{EmployeeName=employee.Attribute("name").Value,EmployeeDeptId=employee.Attribute("deptId").Value,RegionName=employee.Element("Region").Attribute("name").Value,AreaCode=employee.Element("Region").元素t("Area").Attribute("code").Value,}对于多个区域的附加信息,假设列出区域属性:selectnewEmployee{EmployeeName=employee.Attribute("name").Value,EmployeeDeptId=employee.Attribute("deptId").Value,//RegionName=employee.Element("Region").Attribute("name").Value,//AreaCode=employee.Element("Region").Element("Area").Attribute("code").Value,Regions=(fromrinemployee.Elements("Region")选择新区域{Name=r.Attribute("name").Value,Code=r.Element("Area").Attribute("code").Value,}).ToList();您可以在一个查询中选择并在第二个查询中进行过滤,或者将它们组合在一个查询中:new{EmployeeName=employee.Attribute("name").Value,EmployeeDeptId=employee.Attribute("deptId").Value,Regions=fromregioninemployee.Elements("Region")selectnew{Name=region.Attribute("name").Value,AreaCode=region.Element("Area").Attribute("code").价值,}};//现在执行过滤varfilteredEmployees=fromemployeesinemployeesfromregioninemployee.Regionswhereregion.AreaCode=="96"selectemployee;组合一个查询(相同的输出):varemployees2=fromselectedEmployee2infromemployeeinfromemployeeinxml.Descendants("CompanyInfo").Elements("Employee")selectnew{EmployeeName=employee.Attribute("name").Value,EmployeeDeptId=employee.Attribute("deptId").Value,Regions=fromregioninemployee.Elements("Region")选择新的{Name=region.Attribute("name").Value,AreaCode=region.Element("Area").Attribute("code").Value,}}fromregioninselectedEmployee2.Regionswhereregion.AreaCode=="96"selectselectedEmployee2;但是你应该考虑添加一件小事为了增强稳定性,需要检查元素和属性是否存在,然后选择如下:以上是C#学习教程:ParsingXMLwithLINQtogetthefullcontentofchildelements,如果是对大家有用,需要了解更多C#学习教程,希望大家多多关注——varemployees=fromemployeeinxml.Descendants("CompanyInfo").Elements("Employee")selectnew{EmployeeName=(employee.Attribute("name")!=null)?employee.Attribute("name").Value:string.Empty,EmployeeDeptId=(employee.Attribute("deptId")!=null)?employee.Attribute("deptId").Value:string.Empty,Regions=(employee.Elements("Region")!=null)?从employee.Elements("Region")中的区域选择新的{Name=(region.Attribute("name")!=null)?region.Attribute("name").Value:string.Empty,AreaCode=(region.Element("Area")!=null&®ion.Element("Area").Attribute("code")!=null)?region.Element("Area").Attribute("code").Value:string.Empty,}:null};本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: