如何在LINQ中查询此XML?我发现很难使用XML中的属性和嵌套元素。如果我只想提取具有Type="Mobile"属性的Phone元素并在一行中打印地址,我该如何在LINQ中执行此操作?我想生成这样的输出:332-899-5678|123Main,StMercerIsland,WA68042请帮忙,下面是我的示例xml文件PatrickHines206-555-0144425-555-0145332-899-5678123MainStMercerIslandWA68042stringxml=@"PatrickHines206-555-0144425-555-0145332-899-5678123MainStMercerIslandWA68042DorothyLee910-555-1212330-235-555-000516FriarDuckLnGreensboroNC27410";XDocument文档=XDocument.Parse(xml);varquery=fromcontactindocument.Descendants("Contact")letaddress=contact.Element("Address")选择新的{Name=contact.Element("Name").Value,MobilePhone=contact.Elements("Phone").Where(ele=>ele.Attribute("Type").Value=="Mobile").First().Value,Street1=address.Element("Street1").Value,City=address.Element("City").Value,State=address.Element("State").Value,Postal=address.Element("Postal").Value};foreach(variteminquery){Console.WriteLine("{0}|{1},{2},{3}{4}",item.MobilePhone,item.Street1,item.City,item.State,item.Postal);可能是这样的:以上是C#学习教程:HowtoquerythisXMLinLINQ?分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注——varrows=fromcontactinroot.Elements("Contact")letphone=(string)contact.Elements("Phone").FirstOrDefault((string)p=>p.Attribute("Type")=="Mobile")letaddr=contact.Element("Address")选择电话+"|"+(字符串)地址。Element("Street1")+"|"+(string)addr.Element("City")+"|"+...本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。转载请注明出处:
