开发者

XLINQ : how to exclude node in XML using XLINQ

开发者 https://www.devze.com 2022-12-20 04:50 出处:网络
hi i have the following xml <students> <student> <id>12</id> <name>Mohsan</name>

hi i have the following xml

<students>
  <student>
    <id>12</id>
    <name>Mohsan</name>
  </student>
  <student>
    &开发者_如何学Pythonlt;id>2</id>
    <name>Ali</name>
    <address>
      <country>Pakistan</country>
    </address>
    <address>
      <country>India</country>
    </address>
    <parent>
      <id>12</id>
      <address>
        <country>Pakistan</country>
      </address>
    </parent>
  </student>
  <student>
    <id>3</id>
    <name>Azhar</name>
  </student>
</students>

i want to get the address of student only. not the address of its parent. i used this query

var stds = from std in doc.Descendants("student")
                select new
                    {
                        ID = std.Element("id").Value,
                        Name = std.Element("name").Value,
                        Address = from addr in std.Descendants("address")
                                    select addr.Element("country").Value
                    };

but this query returning me address of its parent too. which i dont want. note that a student can have multiple address. please tell me how to exclude the parent address at the time of retrieval..


Use std.Elements("address") instead of std.Descendants("address")

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号