c# – 异常:XPath表达式计算为意外类型System.Xml.Linq.XAttrib
发布时间:2020-12-15 08:41:50 所属栏目:百科 来源:网络整理
导读:我有一个像下面这样的 XML文件: Employees Employee Id="ABC001" NamePrasad 1/Name Mobile9986730630/Mobile Address Type="Perminant" CityCity1/City CountryIndia/Country /Address Address Type="Temporary" CityCity2/City CountryIndia/Country /Add
我有一个像下面这样的
XML文件:
<Employees> <Employee Id="ABC001"> <Name>Prasad 1</Name> <Mobile>9986730630</Mobile> <Address Type="Perminant"> <City>City1</City> <Country>India</Country> </Address> <Address Type="Temporary"> <City>City2</City> <Country>India</Country> </Address> </Employee> 现在我想要获取所有地址类型. 我尝试下面使用XPath,我得到例外. var xPathString = @"//Employee/Address/@Type"; doc.XPathSelectElements(xPathString); // doc is XDocument.Load("xml file Path")
我的XPath有什么问题吗? 解决方法
你的XPath很好(虽然你可能希望它更具选择性),但你必须调整你的评估方式……
顾名思义,
var type = ((IEnumerable<object>)doc.XPathEvaluate("//Employee/Address/@Type")) .OfType<XAttribute>() .Single() .Value; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- MVVM without ReactiveCocoa
- Ehcache(02)——ehcache.xml简介
- objective-c – iOS 5 UINavigationBar删除子视图(图像子视
- ruby-on-rails – 如何在Amazon Elastic Beanstalk上部署ra
- c – 为什么std :: queue在std :: deque没有实现insert()的
- Disable XML validation in Eclipse - 解决validation xml慢
- c – resource.h中的宏用于什么?
- 从针对接口编程到依赖注入
- 活用事件触发对象动作
- ajaxFileUpload.js 无刷新上传图片,支持多个参数同时上传