Разбор данных с использованием XDocument не дает результата -Почему? - PullRequest
1 голос
/ 16 февраля 2012

У меня есть xml-файл как под

<!--Sample Person Data-->
<Person>
  <PersonDetail PersonId="1">
    <Name>Priyanka Das</Name>
    <Age>30</Age>
    <Sex>Male</Sex>
    <LivesIn>Bangalore</LivesIn>
    <Email>nb@training.com</Email>
  </PersonDetail>
  <PersonDetail PersonId="2">
    <Name>Shashidhar Nikatani</Name>
    <Age>40</Age>
    <Sex>Male</Sex>
    <LivesIn>Bangalore</LivesIn>
    <Email>sn@training.com</Email>
  </PersonDetail>
  <PersonDetail PersonId="3">
    <Name>Arundhuti Roy</Name>
    <Age>27</Age>
    <Sex>Female</Sex>
    <LivesIn>Kerala</LivesIn>
    <Email>ab@training.com</Email>
  </PersonDetail>
  <PersonDetail PersonId="4">
    <Name>Nitin Mallik</Name>
    <Age>28</Age>
    <Sex>Male</Sex>
    <LivesIn>Bangalore</LivesIn>
    <Email>ng@training.com</Email>
  </PersonDetail>
  <PersonDetail PersonId="5">
    <Name>Essaki Raja Kandaswamy</Name>
    <Age>27</Age>
    <Sex>Male</Sex>
    <LivesIn>Madras</LivesIn>
    <Email>er@training.com</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;

Требуется помощь

1 Ответ

3 голосов
/ 16 февраля 2012

Нет прямых "LivesIn" детей "Person", поэтому data.Element("LivesIn") неизменно ничего не даст.

Вы уверены, что не имели в виду:

from data in xmlSource.Descendants("PersonDetail")
                    where (string)data.Element("LivesIn") == "Bangalore"
                    select data;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...