使用 XDocument 解析数据没有结果 - 为什么?
我有一个 xml 文件,如下所示,
<!--Sample Person Data-->
<Person>
<PersonDetail PersonId="1">
<Name>Priyanka Das</Name>
<Age>30</Age>
<Sex>Male</Sex>
<LivesIn>Bangalore</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="2">
<Name>Shashidhar Nikatani</Name>
<Age>40</Age>
<Sex>Male</Sex>
<LivesIn>Bangalore</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="3">
<Name>Arundhuti Roy</Name>
<Age>27</Age>
<Sex>Female</Sex>
<LivesIn>Kerala</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="4">
<Name>Nitin Mallik</Name>
<Age>28</Age>
<Sex>Male</Sex>
<LivesIn>Bangalore</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="5">
<Name>Essaki Raja Kandaswamy</Name>
<Age>27</Age>
<Sex>Male</Sex>
<LivesIn>Madras</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
</Person>
我需要找出住在班加罗尔的人员的详细信息。
我已完成以下操作,但无法获得任何结果
XDocument xmlSource = null;
xmlSource = XDocument.Load(@"D:\Personsample.xml");
var query = from data in xmlSource.Descendants("Person")
where (string)data.Element("LivesIn") == "Bangalore"
select data;
需要帮助
I have an xml file as under
<!--Sample Person Data-->
<Person>
<PersonDetail PersonId="1">
<Name>Priyanka Das</Name>
<Age>30</Age>
<Sex>Male</Sex>
<LivesIn>Bangalore</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="2">
<Name>Shashidhar Nikatani</Name>
<Age>40</Age>
<Sex>Male</Sex>
<LivesIn>Bangalore</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="3">
<Name>Arundhuti Roy</Name>
<Age>27</Age>
<Sex>Female</Sex>
<LivesIn>Kerala</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="4">
<Name>Nitin Mallik</Name>
<Age>28</Age>
<Sex>Male</Sex>
<LivesIn>Bangalore</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
<PersonDetail PersonId="5">
<Name>Essaki Raja Kandaswamy</Name>
<Age>27</Age>
<Sex>Male</Sex>
<LivesIn>Madras</LivesIn>
<Email>[email protected]</Email>
</PersonDetail>
</Person>
I need to find out the details of Persons who Lives In Bangalore.
I have done the below but not able to get any result
XDocument xmlSource = null;
xmlSource = XDocument.Load(@"D:\Personsample.xml");
var query = from data in xmlSource.Descendants("Person")
where (string)data.Element("LivesIn") == "Bangalore"
select data;
Help needed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“Person”没有直接的“LivesIn”子级,因此 data.Element("LivesIn") 总是不会产生任何结果。
你确定你不是想说:
There are no direct "LivesIn" children of "Person", so
data.Element("LivesIn")
will invariably not yield anything.Are you sure you didn't mean: